PCRE:环视的 DEFINE 语句

PCRE: DEFINE statement for lookarounds

深入正则表达式的世界,我遇到了 PCRE 中的 DEFINE 语句。
我有以下代码(它定义了一个小写字母、一个大写字母和 anA 组(我知道它在这一点上毫无用处,谢谢 :):

(?(DEFINE)
 (?<lowercase>(?=[^a-z]*[a-z])) # lowercase
 (?<uppercase>(?=[^A-Z]*[A-Z])) # uppercase
 (?<anA>A(?=B))
)
^(?&anA)

现在,我想知道如何将前瞻(本例中的 lowercase)与 anA 部分结合起来?诚然,很难找到有关 DEFINE 语法的适当文档。这里是 a regex101.com fiddle.
为了更清楚一点,我希望有机会组合子例程。例如,对于上面的例子(即验证需要有 A 后跟 B 和一些小写字母的密码),我可以 do the following:

^(?=[^a-z]*[a-z]).*?A(?=B).*

如何使用上述子例程完成此操作?

编辑: 作为参考,我最终使用了 following construct

(?(DEFINE)
 (?<lc>(?=[^a-z\n]*[a-z]))      # lowercase
 (?<uc>(?=[^A-Z\n]*[A-Z]))      # uppercase
 (?<digit>(?=[^\d\n]*\d))       # digit
 (?<special>(?=.*[!@]+))        # special character
)
^(?&lc)(?&uc)(?&digit)(?&special).{6,}$

How I can combine the lookahead (lowercase in this example) with the anA part

您可以使用 (?&lowercase) 命名子例程调用以与 anA 相同的方式递归子模式:

/(?(DEFINE)
   (?<lowercase>(?=[^a-z]*[a-z])) # lowercase
   (?<uppercase>(?=[^A-Z]*[A-Z])) # uppercase
   (?<anA>A(?=B))
  )
  ^(?&lowercase)(.*?)((?&anA)).*
/mgx

regex demo。请注意,您需要在 regex101.com 处使用 /x 修饰符指定 VERBOSE/IgnorePatternWhitespace/Freespace 模式,此模式才能工作。

注意一个警告,如果你还想定义 .*.*? 子模式(参见 PCRE Man Pages):

All subroutine calls, whether recursive or not, are always treated as atomic groups. That is, once a subroutine has matched some of the subject string, it is never re-entered, even if it contains untried alternatives and there is a subsequent matching failure. Any capturing parentheses that are set during the subroutine call revert to their previous values afterwards.