Negative lookahead to get last instance of a word
To get the last instance of abc from the below string
abcaaaaaaaabcbbbbabc111111abc22
use a regular expression negative lookahead that searches for abc not followed by any further abc. As Justin Cooney describes in his article, the syntax is:
abc(?!.*abc)
regular-expressions.info provides further information about negative and positive lookahead.
With negative lookahead, to match a q
not followed by a u
use q(?!u)
. Positive lookahead works the same. q(?=u)
matches a q
that is followed by a u
, without making the u
part of the match.