正则表达式模式解释

前端之家收集整理的这篇文章主要介绍了正则表达式模式解释前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我对正则表达式相当新,我最近在perl脚本中使用了一个正则表达式,我无法弄清楚:

$groups= qr/\(([^()]+|(??{$groups}))*\)/;

任何帮助,将不胜感激!

解决方法

好吧,如果你扩展它:

$groups= qr/
  \(                 # match an open paren (
    (                # followed by
      [^()]+         # one or more non-paren character
    |                # OR
      (??{$groups})  # the regex itself
    )*               # repeated zero or more times
  \)                 # followed by a close paren )
/x;

你会得到一个优雅的递归方法来找到平衡的括号:)

猜你在找的正则表达式相关文章