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 by
s
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';
\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
Post a Comment