【发布时间】:2011-03-26 12:24:29
【问题描述】:
我正在尝试匹配从关键字(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。
标签: regex preg-match