我试了下面两个脚本。脚本1得到了我期望的结果。脚本2没有 - 可能停留在while循环中?无法摆脱while循环?
$_= "Now we are engaged in a great civil war; we will be testing whether
that nation or any nation so conceived and so dedicated can long endure. ";
my $count = 0;
while (/we/ig){
$count++
};
print $count;
输出2
$_= "Now we are engaged in a great civil war, we will be testing whether
that nation or any nation so conceived and so dedicated can long endure";
my $count = 0;
while (/we/){
$count++
};
print $count;
我的理解是/g
允许全局匹配。但我只是好奇的脚本2, 后Perl发现$_
和$count
现在等于1,当它回环,因为没有/g
,它是如何响应的第一场比赛“我们”?还是因为不知道如何回应而卡住?
什么是匹配的回报取决于三个事情:/ g或不,列出上下文,以及是否捕获parens(全部在文档中描述) – ysth