Question

I'm trying to figure out how to grab the tail end of a string using a word as a delimiter, but that word can be used anywhere in the string. So only the last use would start the grab.

example: Go by the office and pickup milk by the safeway BY tomorrow

I want to grab the by tomorrow and not the other bys

This is the regex I'm trying to make robust:

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

I think a negative lookahead would do it, but I've never used one before

Thanks!

Was it helpful?

Solution

I'm not sure what are the other things you have in the regex for, but here's the one I would use:

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

regex101 demo

\b is word boundary and will match only between a \w and a \W character or at string beginning/end.

by matches by literally.

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

(?!.*\bby\b) is the negative lookahead and will prevent a match if there is another word by ahead.

.*?$ is to get the remaining part of the string till the end of the string.

OTHER TIPS

To match the last BY (uppercase and lowercase letters) try this regex:

\b[bB][yY]\b(?!.*\b[bB][yY]\b.*)

see demo here http://regex101.com/r/uA2rL0

This uses the \b word boundary to avoid matching things like nearby and as you said a negative lookahead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top