Strange regular expressions #110976
Replies: 2 comments 5 replies
-
You can use the source generator to see the implementation of particular regular expression, and explanation. The explanation of
|
Beta Was this translation helpful? Give feedback.
-
It seems the interpreting engine still has a bug with empty sub-expression in combination with the string input = "ab";
Match m1 = Regex.Match(input, "a()b");
Match m2 = Regex.Match(input, "a()+?b");
Match m3 = Regex.Match(input, "a(){1,}?b");
Match m4 = Regex.Match(input, "a()*?b");
Match m5 = Regex.Match(input, "a(){0,}?b");
Match m6 = Regex.Match(input, "a()+b");
Match m7 = Regex.Match(input, "a()*b");
Console.WriteLine($"{m1.Success} {m1.Value}");
Console.WriteLine($"{m2.Success} {m2.Value}");
Console.WriteLine($"{m3.Success} {m3.Value}");
Console.WriteLine($"{m4.Success} {m4.Value}");
Console.WriteLine($"{m5.Success} {m5.Value}");
Console.WriteLine($"{m6.Success} {m6.Value}");
Console.WriteLine($"{m7.Success} {m7.Value}"); Output using .NET 9 on dotnetfiddle (https://dotnetfiddle.net/WvEuyr):
Note how the result using EDIT: FYI, the issue seems to be with the interpreting regex engine. The issue does not occur using compiled regex expressions (https://dotnetfiddle.net/405nAA). Couldn't check src-gen, because dotnetfiddle. but i am going to assume based on @DL444 post that src-gen regex matching is okay. EDIT2: I mistakenly equated A possibly valueable addendum: |
Beta Was this translation helpful? Give feedback.
-
Why result of Regex.Match("wtfaab","(.).+()+?b") is the last word "b"? and group1 capture the first word 'w'. It's a feature kind of c#?
Beta Was this translation helpful? Give feedback.
All reactions