What matches the regular expression (? <N> a) (b) \ 2 (c)?

What matches this regular expression?

(?<n>a)(b)\2(c) 

It does not match abc , abac , abbc , abcc or ab\x02c and does not throw an exception.


If you uncheck (c) , it will match aba . I understand that unnamed captures are first numbered, and then named captures are captured. So (c) should get 2, except that I am trying to follow the backlink before defining it, so I thought that maybe it will match a and a will be renumbered when it reaches c , but that’s not how seems also.

+4
source share
3 answers

\2 in your regular expression refers to group (c) , as your experiments showed. Unfortunately, your regular expression never matches anything.

You can find the link in the documentation (although this is not too clear and it seems like an unrelated example follows) :

If a group has not written any substrings, the link to this group will be undefined and will never match.

+1
source

It corresponds to "abbc" for me. I use the Perl regex flavor and I get "a" as group 1, "b" as group 2, and c - group 3. the \ 2 refers to the second group, which is "b"

(?<n>a) - Named capture group: name "n" regular expression "a"
(b)\2 - match capture group "b", then reference to the second group, which is "b"
(c) - comparison of the third capture group "c"

+3
source

the back link is "n". b link is "1" and the third is "2"

 (?<n>a)(b)\1(c) matches abbc where n is a, 1 is b and 2 is c 

it displays unnamed backlinks with 1 and counts. Missing parentheses are not numbered. Thus, he cannot match anything.

Regular-Expressions.info in brackets

+2
source

All Articles