正则表达式模式解释

发布于 2024-12-20 09:38:42 字数 138 浏览 1 评论 0原文

我对正则表达式相当陌生,最近我偶然发现了 perl 脚本中的正则表达式,但我无法弄清楚:

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

任何帮助将不胜感激!

I am fairly new to regexes and I stubled upon a regex in perl script recently which I couldn't figure out:

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

Any help would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韶华倾负 2024-12-27 09:38:42

好吧,如果你扩展它:

$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;

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

Well if you expand it :

$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;

You get an elegant recursive approach to find balanced parentheses :)

旧伤还要旧人安 2024-12-27 09:38:42

YAPE::Regex::Explain 模块可以告诉您什么正则表达式正在做:

% perl5.14.2 -MYAPE::Regex::Explain -E 'say YAPE::Regex::Explain->new(shift)->explain' '\(([^()]+|(??{$groups}))*\)'
The regular expression:

(?-imsx:\(([^()]+|(??{$groups}))*\))

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  \(                       '('
----------------------------------------------------------------------
  (                        group and capture to \1 (0 or more times
                           (matching the most amount possible)):
----------------------------------------------------------------------
    [^()]+                   any character except: '(', ')' (1 or
                             more times (matching the most amount
                             possible))
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    (??{$groups})            run this block of Perl code (that isn't
                             interpolated until RIGHT NOW)
----------------------------------------------------------------------
  )*                       end of \1 (NOTE: because you are using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \1)
----------------------------------------------------------------------
  \)                       ')'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

您可以将该单行代码转换为您的个人资料中的别名:

alias re="perl -MYAPE::Regex::Explain -E 'say YAPE::Regex::Explain->new(shift)->explain'"

之后,您不必记住所有这些:

re '\(([^()]+|(??{$groups}))*\)'

The YAPE::Regex::Explain module can tell you what a regex is doing:

% perl5.14.2 -MYAPE::Regex::Explain -E 'say YAPE::Regex::Explain->new(shift)->explain' '\(([^()]+|(??{$groups}))*\)'
The regular expression:

(?-imsx:\(([^()]+|(??{$groups}))*\))

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  \(                       '('
----------------------------------------------------------------------
  (                        group and capture to \1 (0 or more times
                           (matching the most amount possible)):
----------------------------------------------------------------------
    [^()]+                   any character except: '(', ')' (1 or
                             more times (matching the most amount
                             possible))
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    (??{$groups})            run this block of Perl code (that isn't
                             interpolated until RIGHT NOW)
----------------------------------------------------------------------
  )*                       end of \1 (NOTE: because you are using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \1)
----------------------------------------------------------------------
  \)                       ')'
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

You can turn that one-liner into an alias in your profile:

alias re="perl -MYAPE::Regex::Explain -E 'say YAPE::Regex::Explain->new(shift)->explain'"

After that, you don't have to remember all that:

re '\(([^()]+|(??{$groups}))*\)'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文