Perl:为什么这种模式匹配?

时间:2014-04-10 06:50:48

标签: regex perl

为什么以下模式匹配成功?我错过了什么吗?

$a="pattern";
if($a =~ /[0-9]*/){
   print "Contains\n";
}

2 个答案:

答案 0 :(得分:10)

*量词匹配0或更多。并且模式确实与零位匹配。

您可能希望使用+来表示匹配1次或更多次。

引自perldoc perlre

   Quantifiers

   The following standard quantifiers are recognized:

       *           Match 0 or more times
       +           Match 1 or more times
       ?           Match 1 or 0 times
       {n}         Match exactly n times
       {n,}        Match at least n times
       {n,m}       Match at least n but not more than m times

答案 1 :(得分:6)

使用*作为量词意味着或更多实例。在这种情况下,它在目标字符串的p之前的位置处与零匹配。

要匹配至少一位数,请使用+量词。

相关问题