Python (re)

File Extension in PY

Captures the file extension from a filename.

Try it in the PY tester →

Pattern

regexPY
\.([A-Za-z0-9]+)$

Python (re) code

pyPython
import re

pattern = re.compile(r"\.([A-Za-z0-9]+)$")
input_text = "document.pdf"
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

`\.` matches the literal dot. `([A-Za-z0-9]+)$` captures alphanumeric characters up to the end of the string.

Examples

Input

document.pdf

Matches

  • .pdf

Input

archive.tar.gz

Matches

  • .gz

Input

no-extension

No match

Same pattern, other engines

← Back to File Extension overview (all engines)