lookahead - A regex for the last use of a word in a string -


i'm trying figure out how grab tail end of string using word delimiter, word can used anywhere in string. last use start grab.

example: go office , pickup milk safeway tomorrow

i want grab by tomorrow , not other bys

this regex i'm trying make robust:

$pattern = '/^(.*?)(@.*?)?(\sby\s.*?)?(@.*)?$/i'; 

i think negative lookahead it, i've never used 1 before

thanks!

i'm not sure other things have in regex for, here's 1 use:

$pattern = '/\bby\s(?!.*\bby\b).*?$/i'; 

regex101 demo

\b word boundary , match between \w , \w character or @ string beginning/end.

by matches by literally.

\s matches space (also matches newlines, tabs, form feeds, carriage returns)

(?!.*\bby\b) negative lookahead , prevent match if there word by ahead.

.*?$ remaining part of string till end of string.


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -