Python (re)

Markdown Heading in PY

Matches markdown ATX-style headings (# through ######).

Try it in the PY tester →

Pattern

regexPY
^(#{1,6})\s+(.+)$   (flags: gm)

Python (re) code

pyPython
import re

pattern = re.compile(r"^(#{1,6})\s+(.+)$", re.MULTILINE)
input_text = "# Title\n## Subtitle\n### Section"
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

`^(#{1,6})` captures 1–6 hash characters at line start. `\s+` requires whitespace. `(.+)$` captures the heading text to end of line.

Examples

Input

# Title ## Subtitle ### Section

Matches

  • # Title
  • ## Subtitle
  • ### Section

Input

####### Not a heading

No match

Same pattern, other engines

← Back to Markdown Heading overview (all engines)