4

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

Regex Pattern Issue

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.

2
  • Basic question: does it have to be a regular expression? If you are using a programming language, it's probably much easier with a different approach.
    – Andrew
    Jul 23, 2020 at 2:33
  • BTW, check that your 3rd test line says Fine instead of Find.
    – Andrew
    Jul 23, 2020 at 2:51

1 Answer 1

5

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.

2
  • 1
    Just saw this now but thanks for this Andrew - worked perfectly.
    – user9454269
    Jul 23, 2020 at 3:08
  • 1
    @CarySwoveland, it seems he wants to match stuff like Findxxx, so that's why I didn't include any boundary.
    – Andrew
    Jul 23, 2020 at 3:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.