Question: How can I pattern match a specific word (word being Find) whilst ignoring any strings that contain a substring (the word Exclude) anywhere within it?
What I want to do is find all patterns that contain the word Find, and exclude any patterns that contain Exclude anywhere within the string.
What I'm expecting in terms of pattern matching are:
Find - Succeed
ExcludeFind - Fail
FineExclude - Fail
xxxFind - Succeed
Findxxx - Succeed
xxxFindxxx - Succeed
ExcludexxxxxxxxxxFind - Fail
FindxxxxxxxxxxExclude - Fail
ExcludexxxxxxxxxxFindxxxxxxxxxxExclude - Fail
The Regex patterns I've made, which vary in terms of results are:
(\bFind\b) - Only works if Find is the only word
(\w?Find\w?) - Pattern matches any word with Find
((\w?Find\w?)([^Exclude])|([^Exclude])(\w?Find\w?)) - Image below
Looking through various Stack posts, I attempted a more sophisticated approach via Positive and Negative lookbehinds, but it doesn't succeed on any patterns.
(?<!Exclude)(Find)(?<=Exclude)
If it were (?<!Exclude)(Find)
then this works but the only two issues ExcludexxxxxxxxxxFind
and FindxxxxxxxxxxExclude
are also succeeding, which I do not want since these strings contain the "Exclude" word.
I thought a wildcard variant may work (?<!Exclude)?(Find)
or something similar, but again, no patterns are matching for this case.
This seems to work:
^(?!.*Exclude).*Find
It's basically a negative lookahead of Exclude
, and then looks for Find
.
It matches 4 lines: 1, 4, 5 and 6. It doesn't match what comes after "Find", but that may not affect you. If you need it to match the entire string, use this instead, so it also matches anything that comes afterwards:
^(?!.*Exclude).*Find.*$
Done with the help if this other question:
Regex to include certain words but exclude another
I started off with the regex from the accepted answer just replacing the strings and removing the slashes, but I considered it was still unnecessarily complex, so I started removing stuff and this was the smallest working version I could come up with.
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