Advertisement

Pattern library / Validation

Regex to Match File Paths

Pulling a filesystem path out of a log line or a config dump — regex match file path, regex to match file path, windows file path regex — is a question with five different answers online, because "path" hides three shapes: Unix (/home/user/report.pdf), Windows drive (C:\Users\admin\file.docx) and UNC share (\\server\share\report.pdf). The published pattern covers all three with one alternation for the root and a repeating group for the directories: (?:[A-Za-z]:\\|\\\\[A-Za-z0-9._-]+\\|/)(?:[^\s/\\]+[\\/])+[^\s/\\]+ — the first branch takes X:\, the second \\host\, the third a bare /, then (?:[^\s/\\]+[\\/])+ consumes one or more directory/ segments and the final [^\s/\\]+ lands on the file name. Literals, character classes and groups only — no lookahead, no backreference — so it runs unchanged in JavaScript, Python and BigQuery.

The pattern
(?:[A-Za-z]:\\|\\\\[A-Za-z0-9._-]+\\|/)(?:[^\s/\\]+[\\/])+[^\s/\\]+
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
/home/user/report.pdfMatchthe plain Unix case: root /, two directory segments, then the file nameTry →
C:\Users\admin\Desktop\file.docxMatchWindows drive path — branch [A-Za-z]:\ takes C:\, the group walks the backslash-separated directoriesTry →
\\server\share\q4.xlsxMatchUNC share: branch \\host\ matches the two leading backslashes plus the server nameTry →
/var/log/nginx/access.logMatchdeep nesting is just more group iterations — every segment/ is consumed before the file nameTry →
loaded /home/user/report.pdf from diskMatchfind mode locates the path inside a sentence; the match starts at / and stops at the space before fromTry →
https://example.com/xMatchfalse positive: the second / acts as a root and example.com/ becomes a directory — URLs contain slashes too (pitfall 1)Try →
D:/data/file.txtMatchdrive with forward slashes: [A-Za-z]:\ wants a backslash, so the match starts at /data/file.txt and D: sits outside itTry →
C:\Users\Jane Doe\file.txtMatcha space ends the match early: the segment class forbids spaces, so you get C:\Users\Jane (pitfall 2)Try →
/home/user/Matchtrailing slash: the match stops at the last segment (/home/user) — directories are not rejected in find mode, only by the anchored variantTry →
and/orNo matchone slash but no directory segment before a file name — the group needs at least one segment/, so a bare conjunction stays outTry →
/blog/No matchdirectory only: the group can eat blog/ but then nothing is left for the file-name partTry →
file.txtNo matcha bare file name has no root — nothing for the first alternation to anchor onTry →
no paths in this textNo matchno slash, no drive, no UNC share — nothing to matchTry →

Where to use it

JavaScript (find every path in a log line)

const line = 'copied /home/user/report.pdf to C:\\Users\\admin\\Desktop\\file.docx';
const re = /(?:[A-Za-z]:\\|\\\\[A-Za-z0-9._-]+\\|\/)(?:[^\s/\\]+[\\/])+[^\s/\\]+/gu;
[...line.matchAll(re)].map(m => m[0]);
// ['/home/user/report.pdf', 'C:\\Users\\admin\\Desktop\\file.docx']

Python (findall → the paths)

import re
line = 'copied /home/user/report.pdf to C:\\Users\\admin\\file.docx'
PAT = r'(?:[A-Za-z]:\\|\\\\[A-Za-z0-9._-]+\\|/)(?:[^\s/\\]+[\\/])+[^\s/\\]+'
re.findall(PAT, line)
# ['/home/user/report.pdf', 'C:\\Users\\admin\\file.docx']

BigQuery SQL (RE2 — flag the rows that hold a path)

SELECT path,
  REGEXP_CONTAINS(path,
    r'(?:[A-Za-z]:\\|\\\\[A-Za-z0-9._-]+\\|/)(?:[^\s/\\]+[\\/])+[^\s/\\]+') AS is_path
FROM `project.dataset.logs`
-- /home/user/report.pdf → true | file.txt → false

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 a file path with a regex?

Use (?:[A-Za-z]:\\|\\\\[A-Za-z0-9._-]+\\|/)(?:[^\s/\\]+[\\/])+[^\s/\\]+: the alternation anchors on a root (X:\, \\host\ or /), the group takes the directories and the last class takes the file name. Run it with the g flag to find paths inside text ([...line.matchAll(re)].map(m => m[0])), or wrap it in ^…$ to test whether a whole string is a path.

Does it match Windows paths and UNC shares?

Yes — those are the first two branches: [A-Za-z]:\ covers C:\Users\admin\file.docx and \\[A-Za-z0-9._-]+\ covers \\server\share\report.pdf. Two caveats: a drive path written with forward slashes (D:/data/file.txt) matches only from the /, and every backslash must be escaped in your language’s string literal (pitfall 3).

Does this pattern work in GA4, BigQuery and Go (RE2)?

Yes — it uses only literals, character classes, groups and quantifiers: no lookahead, no lookbehind, no backreference, so nothing RE2 refuses. In BigQuery: REGEXP_CONTAINS(path, r'(?:[A-Za-z]:\\|\\\\[A-Za-z0-9._-]+\\|/)(?:[^\s/\\]+[\\/])+[^\s/\\]+'). Note that GA4’s page_path is a URL path, not a filesystem path — the pattern matches those too (/blog/post-1 → match); for GA4-specific rules see GA4 page path regex.

File Extension Regex — Match .pdf, .png, .docx · Extract Path from URL — Regex Capture Group · URL Regex — Match http/https Links (Practical)

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.