我对正则表达式相当新,我最近在perl脚本中偶然发现了一个我无法弄清楚的正则表达式:
$groups= qr/\(([^()]+|(??{$groups}))*\)/;
任何帮助将不胜感激!
答案 0 :(得分:10)
好吧,如果你扩展它:
$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;
您可以通过优雅的递归方法找到平衡的括号:)
答案 1 :(得分:2)
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}))*\)'