获取C#中撇号之间的值

时间:2012-06-16 14:57:18

标签: c# split apostrophe

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

3 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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 sample here.

答案 2 :(得分: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