@@ -1053,7 +1053,7 @@ public void Synchronized_NullGroup_Throws()
1053
1053
[ Theory ]
1054
1054
[ InlineData ( RegexOptions . None ) ]
1055
1055
[ InlineData ( RegexOptions . Compiled ) ]
1056
- public void BalancingGroup_CaptureCountConsistentWithConditional ( RegexOptions options )
1056
+ public void BalancingGroup_ConditionalEvaluationConsistency ( RegexOptions options )
1057
1057
{
1058
1058
string input = "00123xzacvb1" ;
1059
1059
string pattern = @"\d+((?'x'[a-z-[b]]+)).(?<=(?'2-1'(?'x1'..)).{6})b(?(2)(?'Group2Captured'.)|(?'Group2NotCaptured'.))" ;
@@ -1064,9 +1064,40 @@ public void BalancingGroup_CaptureCountConsistentWithConditional(RegexOptions op
1064
1064
Assert . True ( match . Groups [ "Group2Captured" ] . Success ) ;
1065
1065
Assert . False ( match . Groups [ "Group2NotCaptured" ] . Success ) ;
1066
1066
1067
- // Group2 should have capture count consistent with conditional evaluation
1067
+ // Group2 should correctly report success to maintain consistency with conditional evaluation
1068
1068
Assert . True ( match . Groups [ 2 ] . Success ) ;
1069
+
1070
+ // After our fix, the balancing group should always have one capture (the dummy one we added)
1069
1071
Assert . Equal ( 1 , match . Groups [ 2 ] . Captures . Count ) ;
1072
+
1073
+ // The capture should be zero-length at position 0
1074
+ Assert . Equal ( 0 , match . Groups [ 2 ] . Captures [ 0 ] . Index ) ;
1075
+ Assert . Equal ( 0 , match . Groups [ 2 ] . Captures [ 0 ] . Length ) ;
1076
+ }
1077
+
1078
+ [ Theory ]
1079
+ [ InlineData ( RegexOptions . None ) ]
1080
+ [ InlineData ( RegexOptions . Compiled ) ]
1081
+ public void BalancingGroup_SimpleExample ( RegexOptions options )
1082
+ {
1083
+ // Simple example with balancing group
1084
+ string pattern = @"(?'open'a+)(?'close-open'b+)(?(open)no|yes)" ;
1085
+ string input = "aaabbb" ;
1086
+
1087
+ Match match = new Regex ( pattern , options ) . Match ( input ) ;
1088
+
1089
+ // The "yes" branch should be matched because all "open" captures were balanced
1090
+ Assert . Equal ( "aaabbbyes" , match . Value ) ;
1091
+
1092
+ // The "open" group should report success and have a zero-length capture after tidying
1093
+ Assert . True ( match . Groups [ "open" ] . Success ) ;
1094
+ Assert . Equal ( 1 , match . Groups [ "open" ] . Captures . Count ) ;
1095
+ Assert . Equal ( 0 , match . Groups [ "open" ] . Captures [ 0 ] . Length ) ;
1096
+
1097
+ // The "close" group should have real captures
1098
+ Assert . True ( match . Groups [ "close" ] . Success ) ;
1099
+ Assert . Equal ( 1 , match . Groups [ "close" ] . Captures . Count ) ;
1100
+ Assert . Equal ( "bbb" , match . Groups [ "close" ] . Captures [ 0 ] . Value ) ;
1070
1101
}
1071
1102
1072
1103
[ Theory ]
0 commit comments