我試了下面兩個腳本。腳本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