Let's assume I have the following subject:
abcdef ghidef
and I want to match word ending with def not preceeded by abc (in this case it would be ghidef). How can I match this?
When I use:
(?<!abc)def
I'm getting the 2nd def
but I'm not getting ghi
here.
No need of lookbehind. You can use with a negative lookahead:
\b(?!abc)\w*def\b
RegEx Demo
RegEx Breakup:
\b
- assert a word boundary(?!abc)
- is negative lookahead that asserts a word doesn't start with abc
after \b
(word boundary)\w*
- match 0 or more word charactersdef
- ending text of word is def
\b
- word boundary 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