英文:
Why regexp does not find match
问题
The code you provided is in C# and seems to be attempting to use regular expressions to find a match. The desired match appears to be "48,15." If you have any specific questions or need assistance with this code, please let me know.
英文:
Code
var tekst = "Sum km-ta 20% _48,15";
var rida = Regex.Match(tekst, @"(?si)Sum(_)?(\skm-ta)?(\s)?(20%)?(:)?(\s)?(_)?(?<kmta>.*?)\s", RegexOptions.Singleline);
Does not find math. It looks like RegExp is correct but for unknown reason sum is not found.
How to get match 48,15 ?
Using C# in .NET 7
答案1
得分: 1
以下是您要翻译的内容:
问题在于 kmta
组是懒惰的,所以空匹配是可以的。您可以尝试限制组内允许的内容并取消懒惰,另外字符串末尾没有不可见字符 - (?si)Sum(_)?(\skm-ta)?(\s)?(20%)?(:)?(\s)?(_)?(?<kmta>[\d,\.]*)(\s)?
:
var tekst = "Sum km-ta 20% _48,15";
var rida = Regex.Match(tekst, @"(?si)Sum(_)?(\skm-ta)?(\s)?(20%)?(:)?(\s)?(_)?(?<kmta>[\d,\.]*)(\s)?", RegexOptions.Singleline);
var ridaGroup = rida.Groups["kmta"].Value; // 48,15
另外,您也可以只取消懒惰并将 \s
变为可选项:(?si)Sum(_)?(\skm-ta)?(\s)?(20%)?(:)?(\s)?(_)?(?<kmta>.*)\s?
英文:
The problem is that kmta
group is lazy, so empty match is fine. You can try to limit what is allowed inside the group and remove laziness, also there is no invisible characters at the end of the string - (?si)Sum(_)?(\skm-ta)?(\s)?(20%)?(:)?(\s)?(_)?(?<kmta>[\d,\.]*)(\s)?
:
var tekst = "Sum km-ta 20% _48,15";
var rida = Regex.Match(tekst, @"(?si)Sum(_)?(\skm-ta)?(\s)?(20%)?(:)?(\s)?(_)?(?<kmta>[\d,\.]*)(\s)?", RegexOptions.Singleline);
var ridaGroup = rida.Groups["kmta"].Value; // 48,15
Also you can just remove laziness and make the \s
optional: (?si)Sum(_)?(\skm-ta)?(\s)?(20%)?(:)?(\s)?(_)?(?<kmta>.*)\s?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论