How can I write a regular expression to match strings that do not end with a phrase?

I need a PHP regular expression that matches the beginning of a line but does not end with a phrase. Sort of:

$s = 'top bus stop'; $b = preg_match('/^top.*(?!top)$/', $s); 

But one that really works. What do you think?

+6
source share
1 answer

You had it almost right. But the final statement should be (?<!top) a lookbehind statement using the < prefix. Thus, he really looks at the previous three characters before the $ tag.

+9
source

Source: https://habr.com/ru/post/889895/


All Articles