我試圖匹配從最後一次出現的關鍵字(foo或bar)到字符串末尾的所有內容。正則表達式匹配從最後一次出現的任何關鍵字的所有內容
例(一):
// I want to match ' foo do you?';
$source = 'This is foo and this is bar i like foo do you?';
$pattern = '/pattern/';
preg_match($pattern, $source, $matches);
我試過如下:
$pattern = '/((foo|bar) .*)$/';
思考它將匹配的foo
和所有下面的文字中最後一次出現,但它不是第一次出現匹配。
print_r($matches);
/*
Array
(
[0] => foo and this is bar i like foo do you?
[1] => foo and this is bar i like foo do you?
[2] => foo
)
*/
注意我關心的理論和如何做到這一點的推理,所以請添加一些解釋或鏈接到相關解釋,請。
我在示例中使用了PHP,但語言無關緊要。我唯一的標準是該模式符合PCRE。 – xzyfer 2011-03-26 03:46:47