regex – Perl的`(?PARNO)`当它完成后,丢弃自己的命名捕获?

前端之家收集整理的这篇文章主要介绍了regex – Perl的`(?PARNO)`当它完成后,丢弃自己的命名捕获?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
递归正则表达式是否理解命名捕获?在文档中有一个注释(?{{code}}),它是一个独立的子模式,它有自己的捕获集,当子模式完成时被丢弃,并且在(?PARNO)中有一个注释, (?{{code}})。(?PARNO)完成后是否丢弃自己的命名捕获?

我在写关于Perl的递归正则表达式为Mastering Perl. perlre已经有一个平衡括号的例子(我在Matching balanced parenthesis in Perl regex显示),所以我想我会尝试平衡的引号:

#!/usr/bin/perl
# quotes-nested.pl

use v5.10;

$_ =<<'HERE';
He said 'Amelia said "I am a camel"'
HERE

say "Matched!" if m/
    (
        ['"]
            ( 
                (?: 
                    [^'"]+
                    | 
                    ( (?1) ) 
                )* 
            )
        ['"]
    )
    /xg;

print "
1 => $1
2 => $2
3 => $3
4 => $4
5 => $5
";

这工作和两个报价显示在$ 1和$ 3:

Matched!
1 => 'Amelia said "I am a camel"'
2 => Amelia said "I am a camel"
3 => "I am a camel"
4 => 
5 =>

没关系。我明白那个。但是,我不想知道数字。所以,我使第一个捕获组命名捕获,并看看% – 期望看到我之前看到在$ 1和$ 2中的两个子字符串:

use v5.10;

$_ =<<'HERE';
He said 'Amelia said "I am a camel"'
HERE

say "Matched [$+{said}]!" if m/
    (?<said>
        ['"]
            ( 
                (?: 
                    [^'"]+
                    | 
                    (?1) 
                )* 
            )
        ['"]
    )
    /xg;

use Data::Dumper;
print Dumper( \%- );

我只看到第一个:

Matched ['Amelia said "I am a camel"']!
$VAR1 = {
          'said' => [
                      '\'Amelia said "I am a camel"\''
                    ]
        };

我预计(?1)会重复第一个捕获组中的所有内容包括所说的命名捕获。我可以通过命名一个新的捕捉一点:

use v5.10;

$_ =<<'HERE';
He said 'Amelia said "I am a camel"'
HERE

say "Matched [$+{said}]!" if m/
    (?<said>
        ['"]
            ( 
                (?: 
                    [^'"]+
                    | 
                    (?<said> (?1) ) 
                )* 
            )
        ['"]
    )
    /xg;

use Data::Dumper;
print Dumper( \%- );

现在我得到了我的期望:

Matched ['Amelia said "I am a camel"']!
$VAR1 = {
          'said' => [
                      '\'Amelia said "I am a camel"\'','"I am a camel"'
                    ]
        };

我认为我可以通过将命名的捕获移动一个级别来解决这个问题:

use v5.10;

$_ =<<'HERE';
He said 'Amelia said "I am a camel"'
HERE

say "Matched [$+{said}]!" if m/
    (
        (?<said>
        ['"]
            ( 
                (?: 
                    [^'"]+
                    | 
                    (?1)
                )* 
            )
        ['"]
        )
    )
    /xg;

use Data::Dumper;
print Dumper( \%- );

但是,这并没有捕捉到较小的子字符串:

Matched ['Amelia said "I am a camel"']!
$VAR1 = {
          'said' => [
                      '\'Amelia said "I am a camel"\''
                    ]
        };

我认为我理解这一点,但我也知道,有人在这里谁实际触及C代码,使它发生。 原文链接:https://www.f2er.com/Perl/173390.html

猜你在找的Perl相关文章