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

rah*_*hul 1 regex perl

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

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

dev*_*ull 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
Run Code Online (Sandbox Code Playgroud)


Ric*_*ard 6

使用*量词表示零个或多个实例.在这种情况下,它p在目标字符串之前的位置处与零匹配.

要匹配至少一个数字使用+量词.