This is a continuation of this issue .
Take a look at this template:
(o(?1)?o)
It corresponds to any sequence o with a length of 2 n and n โฅ 1.
It works, see regex101.com (word boundaries added for a better demonstration).
Question: Why?
In the following, the description of the string (whether it is a match or not) will simply be highlighted in bold or bold, which describes the length, for example 2 n .
Broken (with spaces added):
( o (?1)? o ) ( )
I do not understand why this does not match 2n , but 2 n , because I would describe the pattern as * undefined number oo , stacked in each other.
Visualization:
No recursion, 2 corresponds to:
oo
One recursion 4 corresponds to:
oo oo
So far so easy.
Two recursions. Obviously wrong, because the pattern does not match 6 :
oo oo oo
But why? It seems to fit the pattern.
I conclude that this is not just a simple pattern that repeats because otherwise 6 will have to be matched.
But according to regular-expressions.info :
(?P<name>[abc])(?1)(?P>name) matches three letters, such as (?P<name>[abc])[abc][abc] .
and
[abc])(?1){3} [...] is equivalent to ([abc])[abc]{3}
So it looks like it is simply relaying the regular expression code without information about the previous match of the capture group.
Can someone explain and perhaps imagine why this pattern matches 2 n and nothing more?
Edit:
The comments said:
I doubt that referring to the capture group within oneself is actually a supported case.
regular-expressions.info mentions a technique:
If you place a call inside the group that it calls, you will have a recursive capture group.