Verify fix for balancing group bug in Regular Expressions (#111161) #122836
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
main PR: Already fixed in main branch
Description
Investigation confirms the balancing group bug is already fixed in the codebase. No changes required.
The bug: In
(?'g1-g2'exp), whenexpmatches content precedingg2's capture,g1.Captures.Countwas 0 but(?(g1)yes|no)matchedyes- an inconsistent state.Root cause:
TransferCapturecomputed negative length when balancing capture preceded balanced group.TidyBalancingtreated this as a balance marker, incorrectly removing the capture.The fix (already present at
RegexRunner.TransferCapture:577-582):Regression tests already exist in
Regex.Match.Tests.cs:BalancingGroup_WithConditional_ConsistentBehavior- exact pattern from bug reportBalancingGroup_IsMatched_ConsistencyBalancingGroup_Various_ScenariosCustomer Impact
None - fix is already in place. Issue can be closed.
Regression
No - this was a longstanding bug, now fixed.
Testing
./build.sh libsanddotnet build /t:testRisk
None - no code changes made.
Package authoring no longer needed in .NET 9
IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older versions.
Original prompt
This section details on the original issue you should resolve
<issue_title>BUG:Some bug in Balancing Group of Regular Expressions</issue_title>
<issue_description>### Description
In the balancing group
(?'g1-g2'exp), when the content matched byexpprecedes the latest capture ofg2,g1.Captures.Countand the actual behavior ofg1are 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 matchyes, 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.
Reproduction Steps
Output:
Expected behavior
Or
Actual behavior
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response</issue_description>
<agent_instructions>Determine whether this is still an issue or whether it can be closed. If it still repros, please fix it. </agent_instructions>
Comments on the Issue (you are @copilot in this section)
@ericstj @stephentoub what is your take on this behavior? Is it expected and we just need to document it better, or should we look into balancing groups deeper to see if there is a bug? The [docs](https://learn.microsoft.com/en-us/dotnet/standard/base-types/grouping-constructs-in-regular-expressions?redirectedfrom=MSDN#balancing-group-definitions) mention that the balancing group `deletes` the previously defined group - is that why this behavior is observed?FWIW I tested the repo on a few different framework versions and it behaves consistently. I also wasn't sure about group numbering, so I added logging to that. Here's the updated source and output: