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
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.