Skip to content

expr: anchor every top-level alternative of a regex - #15829

Open
anubhavgupta121 wants to merge 1 commit into
uutils:mainfrom
anubhavgupta121:expr-anchor-alternation
Open

anubhavgupta121 wants to merge 1 commit into
uutils:mainfrom
anubhavgupta121:expr-anchor-alternation

Conversation

@anubhavgupta121

Copy link
Copy Markdown

Closes #15782

Problem: The implicit start anchor as a bare '^' was added without any other modifications to the regex logic, so with '| ' (or |, which has lower priority than ^, the ^ only applies to the first alternative, and the others can match anywhere in the string.

Fix: The fix basically wraps the regex with brackets (non-capturing groups, so the anchor '^' applies to every alternative) and then '^' is added so "^(?: ...)" in build_regex(). It's non-capturing, so any further referencing, such as capture group numbering or back-references like \1, isn't affected.

Tests: added cases where a later alternative matches only after offset 0, which the existing tests didn’t cover.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

expr: only first regex alternative is anchored, others match anywhere

1 participant