开发者

How can I write a regular expression to match lines not ending with a phrase?

开发者 https://www.devze.com 2023-03-11 15:57 出处:网络
I need a PHP regular expression that matches a line beginning, but not ending, with a phrase. Something like:

I need a PHP regular expression that matches a line beginning, but not ending, with a phrase. Something like:

$s = 'top bus stop';
$b = preg_match('/^top.*(?!top)$/', $s);开发者_如何转开发

But one that actually works. What do you think?


You had it almost right. But the final assertion should be (?<!top) a lookbehind assertion using a < prefix. This way it really looks at the preceding three characters before the $ subject end.

0

精彩评论

暂无评论...
验证码 换一张
取 消