Repository navigation
matcher: fix quantifiers that follow a {n,m} repeat - #1306
Merged
brharrington merged 4 commits intoOct 28, 2026
Merged
Conversation
Parser.meta() treated any '?' after a RepeatMatcher as the reluctant
modifier of the repetition. A group whose body ends up as a single
RepeatMatcher, such as (\d{2})?, lost its '?' and became required, so
^v(\d{2})?$ did not match "v". A possessive modifier such as a{2}+ was
parsed as (a{2})+ instead, so ^a{2}+$ matched "aaaa".
Consume the optional '?' or '+' modifier when parsing the {n,m} repeat
so later quantifiers apply to the preceding term as usual.
Fix parsing of `{n,}` which was treated as `{n}` because split drops
the trailing empty string. It is now handled as n repetitions followed
by zero or more, consistent with `+` and `*`, so the tail can still
backtrack. A possessive `{n,}+` uses a RepeatMatcher.
The possessive or reluctant modifier is consumed in repeat() for all
repetition forms. Also fix `++` requiring at least two matches, reject
malformed repetitions such as `a{2,3` or `a{2,3,4}` and a repetition
with nothing to repeat, and check the RE2 repeat limit for nested
repetitions in toPatternExpr.
Since only the yes or no answer for a match is needed, a repetition of
an unbounded repetition can be simplified to a single unbounded
repetition, for example `(a{2,}){2}` to `a{4,}` or `(a+)?b` to `a*b`.
This allows the trailing zero or more to backtrack so it is consistent
with how `+` and `*` behave in other contexts.
RepeatMatcher.rewrite only applied the function to the repeated
matcher, not recursively and not to itself, so optimizer rules and
ignoreCase were skipped for anything within a repetition. For example,
`(?i)(ab|cd){2,}` would not match `ABCD`. rewriteEnd also treated the
repeated portion as being in the final position.
Contributor
|
Thanks for the fix. While reviewing, we found that
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #1305
Parser.meta()treats any?that follows aRepeatMatcheras the reluctant modifier of the repetition and returns the repetition unchanged. A group whose body compiles to a singleRepeatMatcher, such as(\d{2}), takes the same path, so(\d{2})?loses its?and the group becomes required.^v(\d{2})?$does not matchv.A
+after{n,m}is not consumed either, so the possessivea{2}+is parsed as(a{2})+, and^a{2}+$matchesaaaa.Changes
Parser.repeat(), consume an optional?or+modifier directly after the closing}.RepeatMatcheralready matches greedily without backtracking, so both modifiers keep the current matching behavior for the repetition itself.RepeatMatcherspecial case frommeta(), so a?after a group always makes the group optional.repeatInOptionalGroupandrepeatModifierstoAbstractPatternMatcherTest. They compare the results withjava.util.regex.Patternfor all of the matcher test variants.Testing
PatternMatcherTest,CaseInsensitivePatternMatcherTest,ExpandOrPatternMatcherTest,Re2PatternMatcherTest, andRe2CaseInsensitivePatternMatcherTest), for example^v(\d{2})?$ should match vand^a{2}+$ shouldn't match aaaa../gradlew :spectator-api:build(checkstyle, PMD, SpotBugs, license, tests on JDK 8, 17, and 25),./gradlew license, and./gradlew :spectator-reg-atlas:testpass.