File & Path
Windows File Path
Matches absolute Windows file paths (e.g., C:\Users\file.txt).
Try it in RegexPro →Available in
JavaScript / ECMAScript code
jsJavaScript
const re = new RegExp("^[A-Za-z]:\\\\(?:[^\\\\/:*?\"<>|\\r\\n]+\\\\)*[^\\\\/:*?\"<>|\\r\\n]*$", "");
const input = "C:\\Users\\John\\file.txt";
const matches = [...input.matchAll(re)];
console.log(matches.map(m => m[0]));Uses `String.prototype.matchAll` for global iteration (Node 12+ / all modern browsers).
Python (re) code
pyPython
import re
pattern = re.compile(r"^[A-Za-z]:\\\\(?:[^\\\\/:*?\"<>|\\r\\n]+\\\\)*[^\\\\/:*?\"<>|\\r\\n]*$")
input_text = "C:\Users\John\file.txt"
for m in pattern.finditer(input_text):
print(m.group(0))Stdlib `re` module — no third-party dependency. Works on Python 3.6+.
Go (RE2) code
goGo
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[A-Za-z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$`)
input := `C:\Users\John\file.txt`
for _, match := range re.FindAllString(input, -1) {
fmt.Println(match)
}
}Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.
Pattern
regexengine-agnostic
^[A-Za-z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$Raw source: ^[A-Za-z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$
How it works
`^[A-Za-z]:\\` matches the drive letter and root backslash. `(?:[^\\/:*?"<>|\r\n]+\\)*` matches directory segments. Final segment (the file or folder name) is allowed to be empty.
Examples
Input
C:\Users\John\file.txtMatches
C:\Users\John\file.txt
Input
D:\Matches
D:\
Input
/unix/pathNo match
—Common use cases
- •Cross-platform tools
- •File pickers
- •Path validation
Related patterns
Unix File Path
File & PathMatches absolute Unix/Linux file paths.
Relative Path
File & PathMatches relative file paths (./, ../, or path/to/file).
File Extension
File & PathCaptures the file extension from a filename.
Image File Extension
File & PathMatch common image file extensions: jpg, jpeg, png, gif, webp, svg, avif, bmp, ico, tif, tiff.