Anchored expression matches but unanchored doesn't - why?

21 views
Skip to first unread message

David Wahlstedt

unread,
Sep 16, 2024, 12:09:54 PM9/16/24
to PCRE2 discussion list
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

Philip Hazel

unread,
Sep 16, 2024, 12:22:00 PM9/16/24
to David Wahlstedt, PCRE2 discussion list
Those are not the just same patterns with and without ^ anchoring anchoring. The first pattern also has $ on the end; the second does not. Therefore it can match the empty string right at the start, as indicated by the pcre2test output.
Regards,
Philip


--
You received this message because you are subscribed to the Google Groups "PCRE2 discussion list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pcre2-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pcre2-dev/f114195e-6a3e-4c78-8f86-587082e4eb18n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages