regexhelper
Test a pattern. Copy a ready-made one. No signup.

Regex to Match a File Extension

Copy the pattern, paste your filenames below, and see the extension on each one. Below the tester: how to grab just the last extension versus a double extension like .tar.gz.

/ /

What this pattern matches

This expression captures the final extension of a filename — the dot and the letters or digits after it, such as .pdf or .gz. With the multiline flag it works line by line on a list of files.

How it works, part by part

\. — a literal dot.

[A-Za-z0-9]+ — one or more letters or digits, the extension itself.

$ — the end of the line, so only the last extension is taken.

Single versus double extensions

For archive.tar.gz this pattern returns .gz, the true final extension. To capture a compound extension like .tar.gz, use \.[A-Za-z0-9]+(?:\.[A-Za-z0-9]+)?$. Note that a dotfile such as .gitignore is a name, not an extension — anchor on a non-dot character first if you need to exclude those.

Use it in your code

Python
import re
text = "report.pdf\narchive.tar.gz\nphoto.JPEG\nMakefile\n.gitignore"
pattern = r'''\.[A-Za-z0-9]+$'''
for m in re.finditer(pattern, text, re.M):
    print(m.group())
JavaScript
const text = "report.pdf\narchive.tar.gz\nphoto.JPEG\nMakefile\n.gitignore";
const re = new RegExp(String.raw`\.[A-Za-z0-9]+$`, "gm");
console.log(text.match(re));

FAQ

Does it treat .gitignore as an extension?

As written, yes, because it only looks for a dot and letters at the end. Require a character before the dot, like \w\.[A-Za-z0-9]+$, to skip dotfiles.

How do I get .tar.gz as one extension?

Use \.[A-Za-z0-9]+(?:\.[A-Za-z0-9]+)?$ to optionally include a second dotted segment.

Is the match case-sensitive?

The class covers both cases, so .JPEG and .jpeg both match. Lowercase the result in code if you need to compare.