Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›Regex Tester›Regex: File extension
Regex pattern · find & extract

Regex: File extension

Regex to get file extension: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.

Quick answer

Use /\.[A-Za-z0-9]+$/g with the global flag to find every match. In "report.final.pdf" it finds ".pdf". Double extensions such as .tar.gz return only .gz.

Last updated · Maintained by FreeAIToolz · How we test

/\.[A-Za-z0-9]+$/g

0 match(es)

report.final.pdf archive.tar.gz README
Open in the full Regex Tester (replace, flags, groups) →

How this regex works

PartMeaning
\.a literal dot
[A-Za-z0-9]+letters or digits
$at the very end of the name

Flags: g finds every match instead of stopping at the first.

Examples

InputMatches
report.final.pdf.pdf
archive.tar.gz.gz
READMEnone

Every example above is checked automatically in JavaScript and Python each time the site is built.

Limitations and edge cases

Double extensions such as .tar.gz return only .gz. Node’s path.extname and Python’s pathlib.Path.suffix give the same result without regex.

When to use this pattern

Routing uploads by type or building file icons.

Use it in your language

JavaScript

const matches = text.match(/\.[A-Za-z0-9]+$/g) ?? [];

Python

import re
matches = [m.group(0) for m in re.finditer(r'\.[A-Za-z0-9]+$', text)]

PHP

preg_match_all('~\\.[A-Za-z0-9]+$~', $text, $m);
$matches = $m[0];

Java

Matcher m = Pattern.compile("\\.[A-Za-z0-9]+$").matcher(text);
while (m.find()) { System.out.println(m.group()); }

Go

re := regexp.MustCompile(`\.[A-Za-z0-9]+$`)
matches := re.FindAllString(text, -1)

More regex patterns

Frequently asked questions

What is the regex for File extension?
/\.[A-Za-z0-9]+$/g. Double extensions such as .tar.gz return only .gz. Node’s path.extname and Python’s pathlib.Path.suffix give the same result without regex.
Which languages does this pattern work in?
It is tested in JavaScript and Python and uses syntax that also works in PHP (PCRE) and Java. It also works in Go’s RE2 engine.
How do I replace the matches?
In JavaScript use text.replace(/\.[A-Za-z0-9]+$/g, "replacement"); in Python use re.sub(r'\.[A-Za-z0-9]+$', "replacement", text).