Search code examples
regexfull-text-searchnotepad++

RegEx - exclude specified list of strings that contain the string to match


exclude specified list of strings that contain the string to match

i.e.

match any string that contains SON - i.e. SON, SONAR, JSON, SON-O-GRAM, _SON

while at the same time exclude GRANDSON and SONY and any other strings put in the list

I cannot achieve both at the same time. Is it possible to somehow prioritize the exclusion group over the match pattern?

I am looking for simple solution that would work in NP++ and similar ctrl+f searches.


Solution

  • A quick way to achive this is by using the Control Verbs (*SKIP) (*FAIL).

    First you would provide what you don't want to match as what to match and tell the system to Skip those but match in the rest of the text.

    DEMO

    Given your example you would end up with a pattern like this:

    Paste in Notepad++ without starting and ending delimiters ie /

    /\b(?:GRANDSON|SONY)\b(*SKIP)(*FAIL)|\S*SON\S*/
    

    \SSON\S : Meaning match any non space character 0 or more times, then the letters SON and any non space character 0 or more times again.

    Change that to [a-z_]SON[a-z_] etc if you want to be specific with what you want to match, ie alpha or underscore on each side

    In Notepad++ Make sure you have selected the "Regular expression" Search Mode before testing.