Python (re)

Negative Lookahead in PY

Use negative lookahead `(?!...)` to match words that are NOT in a blocklist (here: log-level keywords).

Try it in the PY tester →

Pattern

regexPY
\b(?!error|warn|debug)[a-z]+\b   (flags: gi)

Python (re) code

pyPython
import re

pattern = re.compile(r"\b(?!error|warn|debug)[a-z]+\b", re.IGNORECASE)
input_text = "info user login error timeout debug"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

How the pattern works

\b is a word boundary. (?!error|warn|debug) is a zero-width assertion that fails if the upcoming text is one of the blocked words. [a-z]+ then matches an actual word. The result: every word EXCEPT error/warn/debug. Lookarounds work in JS and Python; Go's RE2 does NOT support them.

Examples

Input

info user login error timeout debug

Matches

  • info
  • user
  • login
  • timeout

Input

all words except WARN here

Matches

  • all
  • words
  • except
  • here

Input

error error error

No match

Same pattern, other engines

← Back to Negative Lookahead overview (all engines)