C#正则表达式匹配

sen*_*ale 3 c# regex

18.jun.7nočiod515,00 EUR

在这里我想用正则表达式得到515,00.

Regex regularExpr = new Regex(@rule.RegularExpression,
                              RegexOptions.Compiled | RegexOptions.Multiline |
                              RegexOptions.IgnoreCase | RegexOptions.Singleline |
                              RegexOptions.IgnorePatternWhitespace);

tagValue.Value = "18.jun. 7 no?i od 515,00 EUR";
Match match = regularExpr.Match(tagValue.Value);

object value = match.Groups[2].Value;
Run Code Online (Sandbox Code Playgroud)

正则表达式是: \d+((.\d+)+(,\d+)?)?

但我总是得到一个空字符串("").如果我在Expresso中尝试这个正则表达式,我得到一个3个值的数组,第三个是515,00.

我的C#代码有什么问题,我得到一个空字符串?

Tim*_*ker 5

你的正则表达式匹配18(因为小数部分是可选的),并且match.Groups[2]指的是(.\d+)应该正确读取(\.\d+)并且没有参与匹配的第二个捕获括号,因此返回空字符串.

您需要更正正则表达式并迭代结果:

StringCollection resultList = new StringCollection();
Regex regexObj = new Regex(@"\d+(?:[.,]\d+)?");
Match matchResult = regexObj.Match(subjectString);
while (matchResult.Success) {
    resultList.Add(matchResult.Value);
    matchResult = matchResult.NextMatch();
} 
Run Code Online (Sandbox Code Playgroud)

resultList[2] 然后会包含你的比赛.