Python (re)

Whitespace-Only Line in PY

Matches lines containing only whitespace (or empty lines).

Try it in the PY tester →

Pattern

regexPY
^\s*$   (flags: gm)

Python (re) code

pyPython
import re

pattern = re.compile(r"^\s*$", re.MULTILINE)
input_text = "line one\n   \n\nline two"
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

`^\s*$` anchors to a full line that contains zero or more whitespace characters and nothing else.

Examples

Input

line one line two

Matches

Same pattern, other engines

← Back to Whitespace-Only Line overview (all engines)