Python (re)

Trailing Whitespace (Per Line) in PY

Match trailing spaces and tabs at the end of each line — the regex linters use to flag dirty whitespace.

Try it in the PY tester →

Pattern

regexPY
[ \t]+$   (flags: gm)

Python (re) code

pyPython
import re

pattern = re.compile(r"[ \t]+$", re.MULTILINE)
input_text = "good line\nbad line  \nfine"
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

[ \t]+ matches one or more space or tab characters. $ with the m (multiline) flag anchors to the end of EACH line, not the whole string. The g flag iterates so a multi-line string surfaces every offending line. Pair with `replace(re, '')` to strip.

Examples

Input

good line\nbad line \nfine

Matches

Input

tab here\tand more\t\t

Matches

  • \t\t

Input

no trailing space

No match

Same pattern, other engines

← Back to Trailing Whitespace (Per Line) overview (all engines)