phpregexrecursionpcreregex-recursion

Backreferences to other recursion levels in PHP / PCRE


I was trying to find the answer on the Net but I was not able to. Does the third section on this page applies to PHP / PCRE or not?

https://www.regular-expressions.info/recursebackref.html

the "Backreferences to other recursion levels part". I am only interested in PHP, so if this does not apply to the PHP language (or possibly JavaScript), this is all that I need to know.

Thanks.


Solution

  • Backreferences with recursion level are not supported by PCRE, the information pertains to Ruby Onigmo regex engine.

    See Onigmo reference:

    \k<n+level>  \k'n+level'
    \k<n-level>  \k'n-level'
    \k<-n+level> \k'-n+level'
    \k<-n-level> \k'-n-level'
    
    \k<name+level> \k'name+level'
    \k<name-level> \k'name-level'
    
    Destine a group on the recursion level relative to the referring position.
    
    ex 1.
    
      /\A(?<a>|.|(?:(?<b>.)\g<a>\k<b>))\z/.match("reee")
      /\A(?<a>|.|(?:(?<b>.)\g<a>\k<b+0>))\z/.match("reer")
    
      \k<b+0> refers to the (?<b>.) on the same recursion level with it.
    

    See a Rubular demo.

    As for PCRE, the only syntax backreferences allow is

     \n              reference by number (can be ambiguous)
     \gn             reference by number
     \g{n}           reference by number
     \g{-n}          relative reference by number
     \k<name>        reference by name (Perl)
     \k'name'        reference by name (Perl)
     \g{name}        reference by name (Perl)
     \k{name}        reference by name (.NET)
     (?P=name)       reference by name (Python)