Why can't Rust regex handle capture groups with the same name? #901
-
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
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 in the same match. I gave one such example in #492, namely, The other issue here is that if multiple groups can have the same name, then that needs to be surface in the API somewhere. Right now, every name is bound to at most one group. What happens if a name is bound to more than one group? How is it actually implemented? AIUI, the only way to achieve this is to impose additional costs on the existing implementation. But maybe I just haven't thought about it enough. Finally, from what I can tell, even if the regex crate did offer something like this, it's not clear that it would do it any smarter and with fewer costs than what someone could do outside of the crate. Namely, you can have So... TL;DR:
|
Beta Was this translation helpful? Give feedback.
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…