我嘗試在PHP中使用正則表達式匹配字符串中的兩個部分。我想,貪婪是有問題的。我希望第一個正則表達式(參見注釋)給我前兩個捕獲,作爲第二個正則表達式,但仍然捕獲這兩個字符串。我究竟做錯了什麼?正則表達式不匹配,貪婪
我試圖獲得+123
(如果cd:
存在,如在第一個字符串中)和456
。
<?php
$data[] = 'longstring start waste cd:+123yz456z longstring';
$data[] = 'longstring start waste +yz456z longstring';
$regexs[] = '/start[^z]*?(cd:([^y]+)y)?[^z]*z([^z]*)z/'; // first
$regexs[] = '/start[^z]*?(cd:([^y]+)y)[^z]*z([^z]*)z/'; // second
foreach ($regexs as $regex) {
foreach ($data as $string) {
if (preg_match($regex, $string, $match)) {
echo "Tried '$regex' on '$string' and got " . implode(',', array_split($match, 1));
echo "\n";
}
}
}
?>
輸出是:
Tried '/start[^z]*?(cd:([^y]+)y)?[^z]*z([^z]*)z/' on 'longstring start waste cd:+123yz456z longstring' and got ,,456
Tried '/start[^z]*?(cd:([^y]+)y)?[^z]*z([^z]*)z/' on 'longstring start waste +yz456z longstring' and got ,,456
Tried '/start[^z]*?(cd:([^y]+)y)[^z]*z([^z]*)z/' on 'longstring start waste cd:+123yz456z longstring' and got cd:+123y,+123,456
沒有第四行因爲cd:
不存在的第二串英寸
預期輸出(因爲我不是專家),其中第一行從實際輸出的區別:
Tried '/start[^z]*?(cd:([^y]+)y)?[^z]*z([^z]*)z/' on 'longstring start waste cd:+123yz456z longstring' and got cd:+123y,+123,456
Tried '/start[^z]*?(cd:([^y]+)y)?[^z]*z([^z]*)z/' on 'longstring start waste +yz456z longstring' and got ,,456
Tried '/start[^z]*?(cd:([^y]+)y)[^z]*z([^z]*)z/' on 'longstring start waste cd:+123yz456z longstring' and got cd:+123y,+123,456
看起來你忘了行輸出的。 – Chriszuma
另外,你能用文字解釋你試圖捕捉什麼嗎?這不是很明顯。 – Chriszuma
@Chriszuma第二個正則表達式與第二個字符串不匹配,因爲該字符串中不存在「cd:」。 – bloodphp