简体   繁体   English

如果使用方括号作为定界符,则Perl regex不起作用。 为什么?

[英]Perl regex no working if using square brackets as delimiter. Why?

I'm very experienced with Perl and regex. 我对Perl和regex很有经验。 However, this is making me crazy, I just can't find an answer to it and I cannot see a reason for it either. 但是,这让我发疯,我只是无法找到答案,我也看不出其原因。 Please look at the following code: 请看下面的代码:

my $str = 'Hello[world]';

say $str =~ m/\w+\[.*?\]/ ? 'Yes' : 'No';
say $str =~ m[\w+\[.*?\]] ? 'Yes' : 'No';
say $str =~ m(\w+\[.*?\]) ? 'Yes' : 'No';

The output of this is: 输出为:

Yes
No
Yes

As you can see, the only thing I'm changing is the regex delimiter, and the expression is not working as I would expect when the delimiter is square brackets. 如您所见,我唯一要更改的是正则表达式定界符,当定界符为方括号时,该表达式无法正常工作。

Can someone please explain why the second one is not matching? 有人可以解释为什么第二个不匹配吗?

Thanks in advance, 提前致谢,

Francisco 弗朗西斯科

The B::Deparse module comes to your rescue: B :: Deparse模块一臂之力

$ perl -MO=Deparse foo.pl
my $str = 'Hello[world]';
say $str =~ /\w+\[.*?\]/u ? 'Yes' : 'No';
say $str =~ /\w+[.*?]/u ? 'Yes' : 'No';
say $str =~ /\w+\[.*?\]/u ? 'Yes' : 'No';
foo.pl syntax OK

As you can see, the escaping of [ ] in your regex meant that perl now interpreted them as meta characters, and not delimiters. 如您所见,在正则表达式中转义[ ]意味着perl现在将它们解释为元字符,而不是定界符。 You need two levels of escape. 您需要两个逃生级别。 Which I am not sure is even possible to do, since \\\\ will be interpreted as literal backslash. 我不确定甚至可以这样做,因为\\\\将被解释为文字反斜杠。

To be extra clear: In a normal regex, the brackets [] have a meta character status. 要特别清楚:在常规正则表达式中,方括号[]具有元字符状态。 So in order to match them literally, they need to be escaped. 因此,为了从字面上匹配它们,需要对其进行转义。 When using them as delimiters, you add another meta character status to them: They are also delimiters. 当将它们用作定界符时,可以向它们添加另一个元字符状态:它们也是定界符。 So both meta character statuses need to be escaped. 因此,两个元字符状态都需要转义。

This will work as intended: 这将按预期工作:

say $str =~ m[\w+\Q\[\E.*?\Q\]\E] ? 'Yes' : 'No';

Of course, the lesson here is to choose your delimiters wisely. 当然,这里的课程是明智地选择定界符。

在执行匹配之前,请尝试用>替换[<] > (或其他类似的替换符号)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM