获取 C# 中撇号之间的值
本文关键字:之间 获取 | 更新日期: 2023-09-27 18:37:21
我想提取撇号之间的值,例如从这个字符串中提取: package: name='com.app' versionCode='4' versionName='1.3'
这就是开发安卓应用程序时"aapt"返回的内容。我必须获取值 com.app
、 4
和 1.3
。我将不胜感激任何帮助:)我找到了这个,但这是 VBA。
此正则表达式应该适用于所有情况,假设'
字符仅作为值的封闭字符出现:
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();
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
}
}
正则表达式英雄示例在这里。
如果您有兴趣,以下是您链接到的 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