Perl:如何以结构化方式获取多个正则表达式捕获?

Perl: How get multiple regex captures in a structured way?

我正在尝试获取任意字符串中一组模式的所有出现,就像这样:

my $STRING = "I have a blue cat. That cat is nice, but also quite old. She is always bored.";

foreach (my @STOPS = $STRING =~ m/(?<FINAL_WORD>\w+)\.\s*(?<FIRST_WORD>\w+)/g ) {

  print Dumper \%+, \@STOPS;
}

但是结果不是我所期望的,我也不完全明白为什么:

$VAR1 = {
          'FINAL_WORD' => 'old',
          'FIRST_WORD' => 'She'
        };
$VAR2 = [
          'cat',
          'That',
          'old',
          'She'
        ];
$VAR1 = {
          'FINAL_WORD' => 'old',
          'FIRST_WORD' => 'She'
        };
$VAR2 = [
          'cat',
          'That',
          'old',
          'She'
        ];
$VAR1 = {
          'FINAL_WORD' => 'old',
          'FIRST_WORD' => 'She'
        };
$VAR2 = [
          'cat',
          'That',
          'old',
          'She'
        ];
$VAR1 = {
          'FINAL_WORD' => 'old',
          'FIRST_WORD' => 'She'
        };
$VAR2 = [
          'cat',
          'That',
          'old',
          'She'
        ];

如果没有更好的解决方案,我最终可以接受 @STOPS 中的内容并省略循环。但我更愿意分别获得每对火柴,但我没有找到办法。

但是为什么循环执行了多次呢?

提前致谢,问候,

迷宫

您需要使用 while 循环而不是 for 循环:

while ($STRING =~ m/(?<FINAL_WORD>\w+)\.\s*(?<FIRST_WORD>\w+)/g ) {
    print Dumper \%+;
}

输出:

$VAR1 = {
          'FIRST_WORD' => 'That',
          'FINAL_WORD' => 'cat'
        };
$VAR1 = {
          'FIRST_WORD' => 'She',
          'FINAL_WORD' => 'old'
        };

for 循环一次收集 @STOPS 中的所有匹配项,并且 %+ 设置为最后一个全局匹配项。 while 循环允许您分别遍历每个全局匹配项。

根据perldoc perlretut

The modifier /g stands for global matching and allows the matching operator to match within a string as many times as possible. In scalar context, successive invocations against a string will have /g jump from match to match, keeping track of position in the string as it goes along. You can get or set the position with the pos() function.