Regex to Match a Windows File Path
Copy the pattern, paste text below, and watch it highlight every Windows path. Below the tester: how the drive letter and folder segments are matched.
Regex pattern
/[A-Za-z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*/g
This matches an absolute Windows file path with a drive letter, e.g. C:\Users\me\file.txt, excluding characters Windows forbids in names. It validates the path format, not that the file exists.
What this pattern matches
This matches an absolute Windows path that starts with a drive letter, followed by backslash-separated folders and an optional file name.
How it works, part by part
[A-Za-z]: — a drive letter and colon, like C:.
\\ — the leading backslash after the drive.
(?:[^\\/:*?"<>|\r\n]+\\)* — zero or more folder names, each followed by a backslash. The character class excludes characters Windows forbids in names.
[^\\/:*?"<>|\r\n]* — the final file or folder name.
UNC and Unix paths
This does not match UNC paths (\\server\share) or Unix paths (/home/jane). Those need their own patterns.
Use it in your code
import re
text = "Open C:\\Users\\Jane\\Documents\\report.docx or D:\\data\\logs\\ . Not a path: /home/jane/file.txt (that is Unix)."
pattern = r'''[A-Za-z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*'''
for m in re.finditer(pattern, text):
print(m.group())
const text = "Open C:\\Users\\Jane\\Documents\\report.docx or D:\\data\\logs\\ . Not a path: /home/jane/file.txt (that is Unix).";
const re = new RegExp(String.raw`[A-Za-z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*`, "g");
console.log(text.match(re));
FAQ
Does it match UNC paths like \\server\share?
No. This matches drive-letter paths only. UNC paths need a separate pattern that allows a double-backslash start.
Does it reject invalid filename characters?
Yes. The character class excludes the characters Windows forbids in names, such as * ? < > and |.