私はいくつかのテキストの "間"をつかむいくつかのコードを持っています。 、具体的には、foo $someword
と次のfoo $someword
の間です。なぜ私のPerl正規表現は無限ループを引き起こしますか?
しかし、最初の "between"に詰まって何らかの形で内部文字列の位置が増えない場合はどうなりますか?
入力データは、ここに改行が付いたテキストファイルです。それらはむしろ無関係ですが、印刷が容易になります。
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";
}
あなたは「/したくありませんg "修飾子も2番目のループにありますか? – jrockway
\ zと\ Zは必要ありません。\ Zには\ zが含まれています。\ z –
私はテキストを歩いていて、配列をつかんでいません(これは/ gが返すものです)。ただし、/ gは最終出力の問題には影響しません。私はもう試した。 :-) –