正则表达式为什么找不到匹配

huangapple go评论58阅读模式
英文:

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?

huangapple
  • 本文由 发表于 2023年5月17日 23:12:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定