Search code examples
regexlistprefix

Regex: How to match substrings not starting with a list of prefixes


I need to replace matching words within a string, but excluding a list of prefixes of those words.

The matchingWord should be replaced in the first line
But excludePrefix1.matchingWord , not in this line
Neither in this other line excludePrefix2.matchingWord
But again in this allowedPrefix.matchingWord

I succeed with a regex with a single prefix:

(?<!(excludePrefix1\.))matchingWord

But how can I do that with several excluded prefixes ?

I tried stuff like :

(?<!((excludePrefix1\.)(excludePrefix2\.)))matchingWord

but that doesn't work, please can a regex expert help us on this tricky matter ?


Solution

  • In several tools negative lookbehind must be fixed length, for your problem use multiple lookbehind:

    (?<!excludePrefix1\.)(?<!excludePrefix2\.)matchingWord