1

我想提取撇号之间的值,例如从这个字符串中提取:package: name='com.app' versionCode='4' versionName='1.3'这是开发 android 应用程序时“aapt”返回的内容。我必须得到值com.app,41.3。我会很感激任何帮助:) 我找到了这个,但是这是 VBA。

4

3 回答 3

3

此正则表达式应适用于所有情况,假设该'字符仅作为值的封闭字符出现:

string input = "package: name='com.app' versionCode='4' versionName='1.3'";
string[] values = Regex.Matches(input, @"'(?<val>.*?)'")
                       .Cast<Match>()
                       .Select(match => match.Groups["val"].Value)
                       .ToArray();
于 2012-06-16T15:03:45.977 回答
1
string strRegex = @"(?<==\')(.*?)(?=\')";
RegexOptions myRegexOptions = RegexOptions.None;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = @"package: name='com.app' versionCode='4' versionName='1.3'";

foreach (Match myMatch in myRegex.Matches(strTargetString))
{
  if (myMatch.Success)
  {
    // Add your code here
  }
}

RegEx Hero 示例在这里。

于 2012-06-16T15:11:33.557 回答
1

如果您有兴趣,这里是您链接到的 VBA 的翻译:

public static void Test1()
{
    string sText = "this {is}  a {test}";
    Regex oRegExp = new Regex(@"{([^\}]+)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
    MatchCollection oMatches = oRegExp.Matches(sText);
    foreach (Match Text in oMatches)
    {
        Console.WriteLine(Text.Value.Substring(1));
    }
}

同样在 VB.NET 中:

Sub Test1()
    Dim sText = "this {is}  a {test}"
    Dim oRegExp = New Regex("{([^\}]+)", RegexOptions.IgnoreCase Or RegexOptions.CultureInvariant)
    Dim oMatches = oRegExp.Matches(sText)
    For Each Text As Match In oMatches
        Console.WriteLine(Mid(Text.Value, 2, Len(Text.Value)))
    Next
End Sub
于 2012-06-16T15:16:29.683 回答