Perl \b词锚定 结果不对 什么原因?代码:my $i=1;my $str="2 4 6 8 10 7";for ($i..10){if ($str=~/\b$i\b/g){print "$i is fine";}else{print "$i is not fine";};$i++;};结果:1 is not fine2 is fine3 is not fine4 is fine5 is not fine6 is fine7
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/24 14:31:27
Perl \b词锚定 结果不对 什么原因?代码:my $i=1;my $str="2 4 6 8 10 7";for ($i..10){if ($str=~/\b$i\b/g){print "$i is fine";}else{print "$i is not fine";};$i++;};结果:1 is not fine2 is fine3 is not fine4 is fine5 is not fine6 is fine7
Perl \b词锚定 结果不对 什么原因?
代码:
my $i=1;
my $str="2 4 6 8 10 7";
for ($i..10)
{
if ($str=~/\b$i\b/g){print "$i is fine";}else{print "$i is not fine";};
$i++;
};
结果:
1 is not fine
2 is fine
3 is not fine
4 is fine
5 is not fine
6 is fine
7 is fine
8 is not fine
9 is not fine
10 is fine
Perl \b词锚定 结果不对 什么原因?代码:my $i=1;my $str="2 4 6 8 10 7";for ($i..10){if ($str=~/\b$i\b/g){print "$i is fine";}else{print "$i is not fine";};$i++;};结果:1 is not fine2 is fine3 is not fine4 is fine5 is not fine6 is fine7
我试了一下,感觉应该是g的问题.你把g去掉就对了.
至于为什么有g的时候不对,应该是有g的时候,每次匹配都是从上次成功匹配后的位置开始,但是因为7已经在字符串最后了,后面没有8,所以匹配失败,导致下次匹配将从字符串的头开始匹配,所以10匹配成功.