在perl中使用正则表达式匹配的奇怪问题,备用尝试匹配

在perl中使用正则表达式匹配的奇怪问题,备用尝试匹配,第1张

概述请考虑以下perl脚本: #!/usr/bin/perl my $str = 'not-found=1,total-found=63,ignored=2'; print "1. matched using regex\n" if ($str =~ m/total-found=(\d+)/g); print "2. matched using regex\n" if ($str =~ m/ 请考虑以下perl脚本:

#!/usr/bin/perl my $str = 'not-found=1,total-found=63,ignored=2'; print "1. matched using regex\n" if ($str =~ m/total-found=(\d+)/g); print "2. matched using regex\n" if ($str =~ m/total-found=(\d+)/g); print "3. matched using regex\n" if ($str =~ m/total-found=(\d+)/g); print "4. matched using regex\n" if ($str =~ m/total-found=(\d+)/g); print "Bye!\n";

运行此后的输出是:

1. matched using regex3. matched using regexBye!

相同的正则表达式匹配一次,之后不匹配.任何想法为什么备用尝试匹配同一个字符串与相同的正则表达式在perl中失败?

谢谢!

解决方法 摆脱m和g作为你的正则表达式的修饰语,他们没有做你想要的.

print "1. matched using regex\n" if ($str =~ /total-found=(\d+)/);print "2. matched using regex\n" if ($str =~ /total-found=(\d+)/);print "3. matched using regex\n" if ($str =~ /total-found=(\d+)/);print "4. matched using regex\n" if ($str =~ /total-found=(\d+)/);

具体来说,m在这种情况下是可选的m / foo /与/ foo /完全相同.真正的问题是g在这种情况下会做一些你不想要的东西.有关详情,请参见perlretut.

总结

以上是内存溢出为你收集整理的在perl中使用正则表达式匹配的奇怪问题,备用尝试匹配全部内容,希望文章能够帮你解决在perl中使用正则表达式匹配的奇怪问题,备用尝试匹配所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1260496.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存