python – Regex a string with a space between words

Starting with your original pattern:

ABC(?!\.png)|ABC(?! thumb\.png)
(Note: Dot is a regex metacharacter and should be escaped with backslash)

This will match ABC which is not followed by .png or ABC not followed by thumb.png. Every possible occurrence of ABC will match this pattern. Therefore, all occurrences of ABC will be match, because every extension will match at least one of the two conditions.

We can write the following correction:

\bABC(?!\.png| thumb\.png)

This pattern says to match:

  • \b word boundary
  • ABC match ABC
  • (?:\.png| thumb\.png) neither .png or thumb.png follows

The negative lookahead used here basically has AND flavored logic, and will exclude both following extensions.

Read more here: Source link