簡體   English   中英

為什么我的Perl正則表達式導致無限循環?

[英]Why does my Perl regex cause an infinite loop?

我有一些代碼可以抓住某些文本的“之間”; 特別是在foo $someword和下一個foo $someword

然而,發生的事情是它被卡在第一個“之間”並且不知何故內部字符串位置不會增加。

輸入數據是一個帶有換行符的文本文件:它們相當無關緊要,但使打印更容易。

my $component = qr'foo (\w+?)\s*?{';

while($text =~ /$component/sg)
{
    push @baz, $1; #grab the $someword
}

my $list = join( "|", @baz);
my $re = qr/$list/; #create a list of $somewords

#Try to grab everything between the foo $somewords; 
# or if there's no $foo someword, grab what's left.

while($text=~/($re)(.+?)foo ($re|\z|\Z)/ms)   
#if I take out s, it doesn't repeat, but nothing gets grabbed.
{
#   print pos($text), "\n";   #this is undef...that's a clue I'm certain.
    print $1, ":", $2; #prints the someword and what was grabbed.
    print "\n", '-' x 20, "\n";
}

更新:還有一個更新來處理要提取的文本中出現的'foo'

use strict;
use warnings;

use File::Slurp;

my $text = read_file \*DATA;

my $marker = 'foo';
my $marker_re = qr/$marker\s+\w+\s*?{/;

while ( $text =~ /$marker_re(.+?)($marker_re|\Z)/gs ) {
    print "---\n$1\n";
    pos $text -= length $2;
}

__DATA__
foo one {
one1
one2
one3

foo two
{ two1 two2
two3 two4 }

that was the second one

foo three { 3
foo 3 foo 3
foo 3
foo foo

foo four{}

輸出:

---

one1
one2
one3


---
 two1 two2
two3 two4 }

that was the second one


---
 3
foo 3 foo 3
foo 3
foo foo


---
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM