RRegexBuilder
Get RegexBuilder

RegexBuilder · regex answers

Regex to extract a file extension

/\.([a-z0-9]+)$/i

The dot must be escaped — bare . means any character. Anchoring at the end takes the last dot, which is what you want for archive.tar.gz: the extension is gz, and the compound suffix is a separate question your code answers, not the pattern.

What each part does

\.a literal dot
([a-z0-9]+)the captured extension
$anchored to the end so only the final dot counts

Tested against real input

Every row below was run through the engine when this page was built. The third column is the substring the pattern actually matched.

InputResultMatched
report.pdfmatches.pdf
archive.tar.gzmatches.gz
IMAGE.PNGmatches.PNG
READMEno match
notes.no match

The mistake to avoid

Writing \.(.*)$ — greedy .* combined with an early dot returns tar.gz for archive.tar.gz on some inputs and nothing sensible on others.

Try it on your own text

Paste this pattern and your input, see each match highlighted with its capture groups, step through what the engine did, and get the equivalent syntax for JavaScript, Python, Java, Go and PCRE.

Open RegexBuilder

Other regex answers