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.
(?:[A-Za-z]:\\|\\\\[A-Za-z0-9._-]+\\|/)(?:[^\s/\\]+[\\/])+[^\s/\\]+Load this pattern into the tester →
How the pattern works
- Three roots, one shape:
[A-Za-z]:\for a drive letter,\\[A-Za-z0-9._-]+\for a UNC host,/for Unix — then(?:[^\s/\\]+[\\/])+takes one or more directory segments and[^\s/\\]+the file name. The segment class rejects both separators (so a segment cannot swallow the next one) and whitespace (so the match stops at the end of the token). - Find mode returns every path in a line:
[...line.matchAll(/(?:[A-Za-z]:\\|\\\\[A-Za-z0-9._-]+\\|/)(?:[^\s/\\]+[\\/])+[^\s/\\]+/gu)].map(m => m[0])gives['/home/user/report.pdf', 'C:\\Users\\admin\\file.docx']for the sample in “Where to use it” below. Drop thegflag or use.exec()for the first path only — and remember a sharedg-flagged regex keepslastIndexbetween calls, so reset it (the tester does) before the next test. - Validation needs the anchored form:
^(?:[A-Za-z]:\\|\\\\[A-Za-z0-9._-]+\\|/)(?:[^\s/\\]+[\\/])+[^\s/\\]+$(the^…$pair forces the whole string to be one path) accepts/home/user/report.pdfand rejects/home/user/,file.txtandhttps://example.com/x. The published unanchored pattern is deliberately the find form — anchored, it could not locate a path inside a log line at all. - Relative paths and drive paths with forward slashes match from their first separator:
logs/2026/error.logyields/2026/error.logandD:/data/file.txtyields/data/file.txt(see examples 6–7). If the leading part matters, prepend the missing root in code, or match the relative form with a second pattern — one pattern cannot tell a relative path from the middle of a sentence.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
/home/user/report.pdf | Match | the plain Unix case: root /, two directory segments, then the file name | Try → |
C:\Users\admin\Desktop\file.docx | Match | Windows drive path — branch [A-Za-z]:\ takes C:\, the group walks the backslash-separated directories | Try → |
\\server\share\q4.xlsx | Match | UNC share: branch \\host\ matches the two leading backslashes plus the server name | Try → |
/var/log/nginx/access.log | Match | deep nesting is just more group iterations — every segment/ is consumed before the file name | Try → |
loaded /home/user/report.pdf from disk | Match | find mode locates the path inside a sentence; the match starts at / and stops at the space before from | Try → |
https://example.com/x | Match | false positive: the second / acts as a root and example.com/ becomes a directory — URLs contain slashes too (pitfall 1) | Try → |
D:/data/file.txt | Match | drive with forward slashes: [A-Za-z]:\ wants a backslash, so the match starts at /data/file.txt and D: sits outside it | Try → |
C:\Users\Jane Doe\file.txt | Match | a space ends the match early: the segment class forbids spaces, so you get C:\Users\Jane (pitfall 2) | Try → |
/home/user/ | Match | trailing slash: the match stops at the last segment (/home/user) — directories are not rejected in find mode, only by the anchored variant | Try → |
and/or | No match | one slash but no directory segment before a file name — the group needs at least one segment/, so a bare conjunction stays out | Try → |
/blog/ | No match | directory only: the group can eat blog/ but then nothing is left for the file-name part | Try → |
file.txt | No match | a bare file name has no root — nothing for the first alternation to anchor on | Try → |
no paths in this text | No match | no slash, no drive, no UNC share — nothing to match | Try → |
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
- URLs match too.
https://example.com/xmatches from its second/— a slash is a slash, and the pattern has no way to know what precedes it (lookbehind would, but RE2 rejects it — see RE2 vs PCRE). For “is this string a path?” use the anchored variant from note 3; for finding paths in text, drop matches containing://in your code. - Spaces end the match early.
C:\Users\Jane Doe\file.txtmatches only up toC:\Users\Jane, because the segment class forbids whitespace — which is exactly what keepsloaded /home/report.pdf from diskfrom swallowing the rest of the sentence. Widening the class to[^/\\]+handlesProgram Filesbut only safe when each line holds a single path. - Backslashes vanish in string literals. Writing
'C:\Users\admin'in JavaScript (or a JSON file) producesC:Usersadmin, because\Uand\aare not escapes you meant. Build the pattern withString.raw/ a regex literal, Python raw strings (r'…') or double every backslash — the tester, Python and BigQuery all show the pattern exactly as published above.
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.
Related patterns
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.