在perl中使用正则表达式匹配的奇怪问题,备用尝试匹配
发布时间:2020-12-16 06:29:17 所属栏目:大数据 来源:网络整理
导读:请考虑以下perl脚本: #!/usr/bin/perl my $str = 'not-found=1,total-found=63,ignored=2'; print "1. matched using regexn" if ($str =~ m/total-found=(d+)/g); print "2. matched using regexn" if ($str =~ m/total-found=(d+)/g); print "3. matc
请考虑以下perl脚本:
#!/usr/bin/perl my $str = 'not-found=1,total-found=63,ignored=2'; print "1. matched using regexn" if ($str =~ m/total-found=(d+)/g); print "2. matched using regexn" if ($str =~ m/total-found=(d+)/g); print "3. matched using regexn" if ($str =~ m/total-found=(d+)/g); print "4. matched using regexn" if ($str =~ m/total-found=(d+)/g); print "Bye!n"; 运行此后的输出是: 1. matched using regex 3. matched using regex Bye! 相同的正则表达式匹配一次,之后不匹配.任何想法为什么备用尝试匹配同一个字符串与相同的正则表达式在perl中失败? 谢谢! 解决方法
摆脱m和g作为你的正则表达式的修饰语,他们没有做你想要的.
print "1. matched using regexn" if ($str =~ /total-found=(d+)/); print "2. matched using regexn" if ($str =~ /total-found=(d+)/); print "3. matched using regexn" if ($str =~ /total-found=(d+)/); print "4. matched using regexn" if ($str =~ /total-found=(d+)/); 具体来说,m在这种情况下是可选的m / foo /与/ foo /完全相同.真正的问题是g在这种情况下会做一些你不想要的东西.有关详情,请参见perlretut. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |