Reputation: 633
I am trying to create a regex to find the following string:
AGK-XL.
Sometimes before and after this string there are other characters that are usually harmless, except if there is the following pattern before the string:
NOT-
I need to delete/ignore those cases. This is what I have tried:
^[^N][^O][^T][^\-]AGK-XL\.(\s|\W|$)
But it only seems to match when there are exactly 4 letters in front of the string. How can I express that any other pattern besides NOT-
before AGK-XL.
is harmless?
Thanks for any hints. edit: I am using regex in VBA atm.
Upvotes: 1
Views: 69
Reputation: 626738
If you cannot use fancy look-behinds, you can rely on capturing mechanism when you need to match something we do not want, and match and capture what you want. See the The Best Regex Trick Ever at rexegg.com.
However, in this case, you can match and capture NOT-AGK-XL.
(so that you can restore it later with $1
backreference), and only match all other occurrences of AGK-XL.
that you will remove. Use alternation operator |
to match both alternatives:
(NOT-AGK-XL\.(?!\w))|AGK-XL\.(?!\w)
See demo
Note I replaced (\s|\W|$)
with (?!\w)
that is - IMHO - a better word boundary check.
Upvotes: 2