-
-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue with multiple named group #216
Comments
Referring to captured text in replacement string is not supported in PHP (and R also). PCRE library itself also doesn't have a straightforward replacement API. However, it seems preg wrapper in PHP works in this case, if you use preg_replace_callback: So it is worth looking into the implementation of this function in PHP: |
You can't indeed refer to a capture by its name in a replacement pattern, however all named captures are at the same time a numbered capture, and you can always use the associated number**(s)**. About the When you use it, you are allowed to use the same name for several captures. It is useful if you want to add semantic to your pattern and it is handy if you need to use a backreference inside the pattern. This feature highlights an issue related to how pcre manages duplicated named captures. Named captures are only aliases for one group and for one match. But if you perform a global research the named capture is alternately the first or the second capture group (in your pattern). So you can't define a bijective relation between a name and a number. If you use The only way to obtain a many-to-one relation between numbered groups and a named group in your case is to use the branch reset feature that gives to the two groups the same number:
As an aside, you can easily design your pattern differently to avoid the problem. |
As previously discussed, this is a tricky edge case. Adjusting it one way or the other will only eliminate one case and introduce another. This is all custom implementation by me, as PCRE has no support for any form of substitution or such. Every language has their own way of doing it. To be safe, you should design your pattern more carefully to avoid falling into problems like these. |
I just read this in the perldoc:
|
|
In this example:
https://regex101.com/r/tQ5qT2/2
The match 15 is never treated
The text was updated successfully, but these errors were encountered: