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?
To expand the regex to match a complete line, add ‹ . * › at both ends. The dot-asterisk sequences match zero or more characters within the current line.
So use \s\S, which will match ALL characters.
End of String or Line: $ The $ anchor specifies that the preceding pattern must occur at the end of the input string, or before \n at the end of the input string. If you use $ with the RegexOptions. Multiline option, the match can also occur at the end of a line.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With