反向引用PHP / PCRE中的其他递归级别

时间:2018-01-10 10:26:42

标签: php regex recursion pcre regex-recursion

我试图在网上找到答案,但我无法做到。此页面上的第三部分是否适用于PHP / PCRE?

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

"反向引用其他递归级别"。我只对PHP感兴趣,所以如果这不适用于PHP语言(或可能是JavaScript),这就是我需要知道的全部内容。

感谢。

1 个答案:

答案 0 :(得分:0)

PCRE不支持递归级别的反向引用,该信息与Ruby Onigmo正则表达式引擎有关。

请参阅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.

查看Rubular demo

对于PCRE,唯一允许的语法反向引用是

 \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)