我试图在 Perl 中提出一个匹配多个模式的正则表达式,并像preg_match_all
在 PHP 中一样返回所有这些模式。
这是我所拥有的:
$str = 'testdatastring';
if($str =~ /(test|data|string)/) {
print "its found index location: $0 $-[0]-$+[0]\n";
print "its found index location: $1 $-[1]-$+[1]\n";
print "its found index location: $2 $-[2]-$+[2]\n";
print "its found index location: $3 $-[3]-$+[3]\n";
}
这只给了我第一场比赛,这是“测试”。我希望能够匹配所有出现的指定模式:“测试”、“数据”和“字符串”。
我知道在 PHP 中,您可以将 preg_match_all 用于这种目的:
if(preg_match_all('/(test|data|string)/', 'testdatastring', $m)) {
echo var_export($m, true);
}
上面的 PHP 代码将匹配所有 3 个字符串:'test'、'data' 和 'string'。
我想知道如何在 Perl 中做到这一点。任何帮助将不胜感激。