Description
Description
In the balancing group (?'g1-g2'exp)
, when the content matched by exp
precedes the latest capture of g2
, g1.Captures.Count
and the actual behavior of g1
are inconsistent.
By checking the captures of the group using Group.Captures
, you will find that the captures appear empty. However, when using (?(g1)yes|no)
for conditional evaluation, it will match yes
, indicating that there actually is a capture.
更多关于平衡组的bug,可以参考平衡组的bug·其二
For more information about this bug, please refer to Bug in Balancing Groups - Part 2
测试用例中,使用到了比较复杂的正则表达式。
复杂的正则表达式,可视化可参考正则可视化与调试工具
In the test cases, more complex regular expressions are used.
For visualizing and debugging complex regular expressions, you can refer to Regex Visualization and Debugging Tool
Reproduction Steps
using System.Text.RegularExpressions;
string input = "00123xzacvb1";
string pattern=@"\d+((?'x'[a-z-[b]]+)).(?<=(?'2-1'(?'x1'..)).{6})b(?(2)(?'Group2Captured'.)|(?'Group2NotCaptured'.))";
try
{
Match matchInterpreted = new Regex(pattern, RegexOptions.None).Match(input);
Console.WriteLine($"Interpreted Group2: {matchInterpreted.Groups[2].Captures.Count}");
Console.WriteLine($"Interpreted Group2Captured: {matchInterpreted.Groups["Group2Captured"].Captures.Count>0}");
Console.WriteLine($"Interpreted Group2NotCaptured: {matchInterpreted.Groups["Group2NotCaptured"].Captures.Count>0}");
}catch(Exception ex)
{
Console.WriteLine($"Interpreted Exception: {ex.Message}");
}
try
{
Match matchCompiled = new Regex(pattern, RegexOptions.Compiled).Match(input);
Console.WriteLine($"Compiled Group2: {matchCompiled.Groups[2].Captures.Count}");
Console.WriteLine($"Compiled Group2Captured: {matchCompiled.Groups["Group2Captured"].Captures.Count>0}");
Console.WriteLine($"Compiled Group2NotCaptured: {matchCompiled.Groups["Group2NotCaptured"].Captures.Count>0}");
}catch(Exception ex)
{
Console.WriteLine($"Compiled Exception: {ex.Message}");
}
Output:
Interpreted Group2: 0
Interpreted Group2Captured: True
Interpreted Group2NotCaptured: False
Compiled Group2: 0
Compiled Group2Captured: True
Compiled Group2NotCaptured: False
Expected behavior
Interpreted Group2: 1
Interpreted Group2Captured: True
Interpreted Group2NotCaptured: False
Compiled Group2: 1
Compiled Group2Captured: True
Compiled Group2NotCaptured: False
Or
Interpreted Group2: 0
Interpreted Group2Captured: False
Interpreted Group2NotCaptured: True
Compiled Group2: 0
Compiled Group2Captured: False
Compiled Group2NotCaptured: True
Actual behavior
Interpreted Group2: 0
Interpreted Group2Captured: True
Interpreted Group2NotCaptured: False
Compiled Group2: 0
Compiled Group2Captured: True
Compiled Group2NotCaptured: False
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response