我们在某些代码中发现了一个 bug,程序员使用了等价于
(.)+
的表达式,但实际上应该使用 (.+)
。这是一个很容易修复的问题,但我们无法解释为什么 (.)+
匹配到了正则表达式中字母 "a" 后面的第一个字母 "e" 而不是 "b"。有谁能够解释一下为什么会出现这种情况?另外,如何解释 (.)+
这个表达式?my $s = 'abcde';
if ($s =~ m{ a (.)+ }x ){
print "s '$s' matched '$1'\n";
}else{
print "total match fail\n";
}
__END__
output:
s 'abcde' matched 'e'
use re 'debug';
。这对于跟踪正在发生的情况非常有用。 - Sobrique