Advertisement

Pattern library / Extract & Replace

File Extension Regex

Whether you are filtering downloads in analytics, auditing a media library or parsing filenames, the shape is the same: a literal dot followed by letters at the end of the string. The anchored version validates; the unanchored version extracts.

The pattern
\.([a-zA-Z0-9]+)$
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
/docs/ga4-cheatsheet.pdfMatchcaptures pdfTry →
photo_v2.PNGMatchcase-insensitive lettersTry →
archive.tar.gzMatchcaptures last ext: gzTry →
READMENo matchno extensionTry →
file.name.No matchempty extensionTry →
/v1.2/usersNo matchdot not at endTry →

Where to use it

JavaScript

const m = '/docs/report.pdf'.match(/\.([a-zA-Z0-9]+)$/);
const ext = m && m[1].toLowerCase();  // 'pdf'

Python (media audit)

import re, os
exts = [m.group(1) for m in (re.search(r'\.([a-zA-Z0-9]+)$', f) for f in files) if m]

BigQuery (downloads report)

WHERE REGEXP_CONTAINS(page_path, r'\.(pdf|docx?|xlsx?|csv)$')

Common mistakes

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

FAQ

How do I match several extensions at once?

Alternation with a whitelist: \.(pdf|docx?|xlsx?|csv)$ — and add the i flag for case-insensitive matching.

Why does archive.tar.gz give me gz?

Because $ anchors to the end and the pattern takes the final dot-run. To treat tar.gz as a unit, match it explicitly: \.(tar\.gz|tgz)$.

How do I ignore query strings?

Strip before matching: value.split(/[?#]/)[0], then apply the extension pattern.

Extract Domain from URL — Regex Capture Group · URL Regex — Match http/https Links (Practical) · Remove Query Parameters with Regex (Replace Example)

For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.