A negative lookahead wont work because - with the *?
(lazy zero-or-more) quantifier - it'll check at S
and find no Impl
immediately ahead so the match succeeds.
If you changed to a greedy quantifier (change *
to *?
) it would still fail because you would be at the end of the string - there is no "ahead" to look at.
Instead you want to use a negative lookbehind to check the previous content, i.e.
[A-Z][A-Za-z\d]*(?<!Impl)
(You may or not want $
at the end of that, depending on where the pattern is used)
Side note: not directly relevant here, but useful to know is that (in Java regex) a lookbehind must have a limited width - you can't use *
or +
quantifiers, and must instead use limited numerical quantifiers, e.g. {0,99}
But unless the pattern itself is important (or you're working in a context where you must use regex), a simpler option would be String.endsWith, like so:
! mystring.endsWith('Impl')