Skip to content

Why can't Rust regex handle capture groups with the same name? #901

Answered by BurntSushi
yangwentong asked this question in Q&A
Discussion options

You must be logged in to vote

See: #492

This is a good question and it's a little tricky to answer. The main thrust of this is that the example you've put forward looks reasonable and have an obvious semantic: there are two different groups (with different capture group indices) with the same name, but the construction of the regex is such that when a match occurs, only one of those groups will contain a matching span. The other is guaranteed to be empty. This is because of how alternations work. In any given match, only one of the branches of an alternation is taken. However... This is not always true.

Namely, if an alternation is inside a repetition operator, then it's possible for multiple branches to participate i…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@yangwentong
Comment options

Answer selected by yangwentong
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants