我想要匹配 my @array = ( 'Tree' , 'JoeTree'); foreach (@array ) { if ( $_ =~ /^(Joe)Tree/gi) { print "matched $_"; } } 它只匹配JoeTree,不匹配Tree?
尝试:if (/^(?:Joe)?Tree/gi) 我们已经将 Joe 部分改为可选项。 同时,您可以将 (..) 更改为 (?:...),因为您只是分组。 $_ =~ 部分也是冗余的,因为默认情况下我们会在 $_ 中检查