为什么在正则表达式中需要 (?(R)...|...) if 条件?

Why would you ever need (?(R)...|...) if condition in a regex?

我正在查看一些正则表达式文档,但被某些东西弄糊涂了。 (?(R)...|...) 上下文中的 (R) 条件据说是:

perl 有点神秘:

(R)

    Checks if the expression has been evaluated inside of recursion. Full syntax: (?(R)then|else)

PCRE 没什么用:

(?(R) overall recursion condition

regular-expressions.info对此无话可说。

这个条件是说子程序栈是否超过1级深还是其他意思?

this explanation:

if there is no subpattern named 'R', the condition is true if a recursive call to the whole pattern or any subpattern has been made

这意味着 (?(R) 条件检查整个模式是否被递归 至少一次 ,并且检查的结果是布尔值,如果发生递归,则为 True,否则为 False

如果您需要查看一些示例,请参阅 https://github.com/PhilipHazel/pcre2/blob/587b94277b50ababde2380b5877c93e36ca65db8/src/pcre2_jit_test.c