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.
| Input | Result | Matched |
|---|---|---|
| report.pdf | matches | |
| archive.tar.gz | matches | .gz |
| IMAGE.PNG | matches | .PNG |
| README | no 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 RegexBuilderOther regex answers
- Regex for an email address
- Regex for a US phone number
- Regex for an IPv4 address
- Regex for a URL
- Regex for a date in YYYY-MM-DD format
- Regex to match digits only
- Regex for alphanumeric characters
- Regex to find special characters
- Regex for a strong password
- Regex to match whitespace
- Regex to trim leading and trailing whitespace
- Regex for a UUID