我想在Perl中找到一個匹配多個模式的正則表達式,並在PHP中返回像preg_match_all
所有的模式。如何在Perl中進行全局正則表達式匹配?
這是我有:
$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";
}
這只是給我的第一場比賽在此爲「測試」。我希望能夠匹配所有出現的指定模式:'test','data'和'string'。
我知道,在PHP中,你可以使用preg_match_all用於這種目的:
if(preg_match_all('/(test|data|string)/', 'testdatastring', $m)) {
echo var_export($m, true);
}
上面的PHP代碼將匹配所有3個字符串:「測試」,「數據」和「串」。
我想知道如何在Perl中做到這一點。任何幫助將不勝感激。
你如何在if/else語句中應用這個? – Carel 2012-03-19 08:10:50