I tried to shrink an example from previous discussions, I didn't quite understand:
With pcre2test
^()(?|()|(a))\2$
does match 'aa'.
PCRE2 version 10.45-DEV 2024-06-09 (8-bit)
/^()(?|()|(a))\2$/debug
------------------------------------------------------------------
0 43 Bra
3 ^
4 5 CBra 1
9 5 Ket
12 11 Bra
15 5 CBra 2
20 5 Ket
23 13 Alt
26 7 CBra 2
31 a
33 7 Ket
36 24 Ket
39 \2
42 $
43 43 Ket
46 End
------------------------------------------------------------------
Capture group count = 2
Max back reference = 2
May match empty string
Compile options: <none>
Overall options: anchored
Subject length lower bound = 0
aa
0: aa
1:
2: a
But
()(?|()|(a))\2
does not:
PCRE2 version 10.45-DEV 2024-06-09 (8-bit)
/()(?|()|(a))\2/debug
------------------------------------------------------------------
0 41 Bra
3 5 CBra 1
8 5 Ket
11 11 Bra
14 5 CBra 2
19 5 Ket
22 13 Alt
25 7 CBra 2
30 a
32 7 Ket
35 24 Ket
38 \2
41 41 Ket
44 End
------------------------------------------------------------------
Capture group count = 2
Max back reference = 2
May match empty string
Subject length lower bound = 0
aa
0:
1:
2:
I thought I understood it before, because the empty string matching the () inside the `(?|` would "steal" the reference \2, but obviously I got it wrong.
Sorry if my questions take too much bandwith, I'm ok with it getting rejected if so.
BR,
David