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.pdfMatches
.pdf
Input
archive.tar.gzMatches
.gz
Input
no-extensionNo match
—