PatternRegex

Extract a file extension

Pull the extension off the end of a filename — anchored to the end of the string so a name with several dots still yields just the last one.

Last updated

Pattern
\.[A-Za-z0-9]+$
Open in Regex Tester & Builder

Matches

  • archive.tar.gz
  • report.docx
  • .env

Doesn’t match

  • no-extension-here
  • trailing.dot.

Why it’s written this way

\.[A-Za-z0-9]+ matches a literal dot followed by one or more letters or digits, and anchoring it with $ forces that run to sit at the very end of the string — which is what makes it an extension matcher rather than just 'find a dot'.

Because it is not global and not looking for the first dot, a filename with multiple dots only ever yields the final segment, which matches how extensions are actually interpreted by most tools.

Edge cases to know

  • →Multi-part extensions like .tar.gz are not recognized as a unit — this pattern returns only .gz off archive.tar.gz, so anything that specifically needs the compound extension must handle that case separately.
  • →Dotfiles with no separate extension, like .env or .gitignore, match the whole thing as if the filename itself were the extension, since the pattern can't tell 'hidden file' from 'name.ext' apart.
  • →A filename ending in a bare dot with nothing after it, like trailing.dot., will not match at all, since + requires at least one character after the dot.

Part by part

\.
An escaped dot. Unescaped it would match any character, and the pattern would return the last character of every filename.
[A-Za-z0-9]+
The extension itself: letters and digits, one or more. Excluding punctuation is what stops it running backwards into the filename.
$
Anchored to the end, which is what makes it find the LAST dot rather than the first — the difference between .gz and .tar.gz.

Common mistakes

  • \.[A-Za-z0-9]+

    Without the end anchor and with a global flag this returns every dotted segment, so archive.tar.gz yields both .tar and .gz. Which one you get without the flag depends on the engine, which is worse.

    \.[A-Za-z0-9]+$
  • \.\w+$

    The word class includes the underscore, so a file named report_final_v2 with no extension at all returns _v2 as though it were one.

  • \.[A-Za-z0-9]{3}$

    Fixing the length at three belongs to an era of eight-dot-three filenames. It misses .html, .jpeg, .md and .webp — most of the extensions on a modern machine.

Questions

How do I get the full .tar.gz rather than just .gz?

This pattern cannot, by design — the end anchor makes it take the final segment. Compound extensions are a naming convention rather than a filesystem feature, so match against a known list of them before falling back to this.

What does it return for a dotfile like .gitignore?

The whole name, because the string is a dot followed by letters at the end. Check for a leading dot first if you are distinguishing hidden files from extensions — on Unix they are the same shape.

Does the extension tell me the file type?

Only what the name claims. An extension is metadata a user can change freely; the actual type is determined by the file's leading bytes. Anything security-relevant should read the magic number instead.

Should I lowercase the result?

Usually yes, since .JPG and .jpg mean the same thing and Windows filesystems are case-insensitive about it. Compare in lowercase and keep the original for display.

Related in Patterns