简体   繁体   English

如何编写正则表达式来匹配不以短语结尾的行?

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

I need a PHP regular expression that matches a line beginning, but not ending, with a phrase.我需要一个 PHP 正则表达式,它与一个短语开头但不结尾的行匹配。 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.但最终的断言应该是(?<!top)使用<前缀的后向断言。 This way it really looks at the preceding three characters before the $ subject end.这样,它实际上会查看$主题结尾之前的前三个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM