[英]Perl greedy regex is not acting greedy
Giving the following code: 提供以下代码:
use strict;
use warnings;
my $text = "asdf(blablabla)";
$text =~ s/(.*?)\((.*)\)/$2/;
print "\nfirst match: $1";
print "\nsecond match: $2";
I expected that $2
would catch my last bracket, yet my output is: 我期望
$2
会抓住我的最后一个括号,但是我的输出是:
If .*
by default it's greedy why it stopped at the bracket? 如果默认情况下是
.*
,那是贪婪的原因,为什么它停在了括号里?
The .*
is a greedy subpattern, but it does not account for grouping. .*
是贪婪的子模式,但不考虑分组。 Grouping is defined with a pair of unescaped parentheses (see Use Parentheses for Grouping and Capturing ). 分组是用一对未转义的括号定义的(请参阅使用括号进行分组和捕获 )。
See where your group boundaries are: 查看组边界在哪里:
s/(.*?)\((.*)\)/$2/
| G1| |G2|
So, the \\(
and \\)
matching (
and )
are outside the groups , and will not be part of neither $1
nor $2
. 因此,匹配的
\\(
和\\)
(
和)
在组之外 ,并且不会成为$1
和$2
。
If you need the )
be part of $2
, use 如果您需要
)
作为$2
一部分,请使用
s/(.*?)\((.*\))/$2/
^
A regex engine is processing both the string and the pattern from left to right. 正则表达式引擎从左到右同时处理字符串和模式。 The first
(.*?)
is handled first, and it matches up to the first literal (
symbol as it is lazy (matches as few chars as possible before it can return a valid match), and the whole part before the (
is placed into Group 1 stack. Then, the (
is matched, but not captured, then (.*)
matches any 0+ characters other than a newline up to the last )
symbol, and places the capture into Group 2. Then, the )
is just matched. The point is that .*
grabs the whole string up to the end, but then backtracking happens since the engine tries to accommodate for the final )
in the pattern. 首先处理第一个
(.*?)
,它与第一个文字匹配(
因为它是惰性的符号(在可以返回有效匹配之前,请尽可能少地匹配字符),以及放在(
之前的整个部分)成第1组堆叠。然后,将(
匹配,但不是捕获的,然后(.*)
大于换行到最后其他任何0+字符匹配)
符号,并放置到捕获组2然后, )
是只是相匹配。问题的关键是, .*
抓住整个字符串到最后,但随后发生回溯自引擎试图以适应最终)
的格局。 The )
must be matched, but not captured in your pattern, thus, it is not part of Group 2 due to the group boundary placement. )
必须匹配,但不能在您的模式中捕获,因此,由于组边界位置,它不属于组2。 You can see the regex debugger at this regex demo page to see how the pattern matches your string. 您可以在此regex演示页面上看到regex调试器 ,以了解模式如何与您的字符串匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.