File Path (Windows) Regex Pattern
Validates Windows file paths with drive letter, backslash separators, and restrictions on invalid characters.
Live Regex Tester
Pattern Breakdown
Code Examples
JavaScript
const regex = /^[a-zA-Z]:\\(?:[^\\/:*?"<>|]+\\)*[^\\/:*?"<>|]*$/; const test = "C:\Users\Documents\file.txt"; console.log(regex.test(test)); // true // Extract matches const matches = test.match(regex); console.log(matches);
Python
import re pattern = r'^[a-zA-Z]:\\(?:[^\\/:*?"<>|]+\\)*[^\\/:*?"<>|]*$' test = "C:\Users\Documents\file.txt" match = re.search(pattern, test) print(match) # Found!
Go
package main
import (
"fmt"
"regexp"
)
func main() {
re := regexp.MustCompile(`^[a-zA-Z]:\\(?:[^\\/:*?"<>|]+\\)*[^\\/:*?"<>|]*$`)
fmt.Println(re.MatchString("C:\Users\Documents\file.txt")) // true
}Common Use Cases
Match Examples
| Input | Result |
|---|---|
| C:\Users\Documents\file.txt | Match |
| /usr/local/bin | No Match |
About the File Path (Windows) Regex
Validates Windows file paths with drive letter, backslash separators, and restrictions on invalid characters.
Regular expressions (regex) are powerful pattern matching tools used across virtually all programming languages. The file path (windows) pattern is classified as intermediate difficulty in the file & path category. It works in all major programming languages.
When using this regex, always consider edge cases and test thoroughly with real-world data. Use the interactive tester above to validate the pattern against your specific inputs before deploying to production.
More File & Path Patterns
Need More Regex Patterns?
Browse our complete library of 100+ regex patterns with interactive testers.
Frequently Asked Questions
What is the File Path (Windows) regex pattern?
Validates Windows file paths with drive letter, backslash separators, and restrictions on invalid characters.
How do I use the File Path (Windows) regex?
Use the pattern /^[a-zA-Z]:\\(?:[^\\/:*?"<>|]+\\)*[^\\/:*?"<>|]*$/ in your code. In JavaScript: new RegExp('^[a-zA-Z]:\\(?:[^\\/:*?"<>|]+\\)*[^\\/:*?"<>|]*$', ''). Test it above with your own input.
What does this File Path (Windows) regex match?
This pattern matches: "C:\Users\Documents\file.txt". It does NOT match: "/usr/local/bin". Windows path validation, cross-platform tools, file operations.
Is the File Path (Windows) regex beginner-friendly?
This pattern is rated Intermediate. It uses some advanced features like character classes and quantifiers.
What languages support the File Path (Windows) regex?
This pattern works in all major programming languages including JavaScript, Python, Java, C#, Go, Ruby, PHP, and more. Syntax may vary slightly between regex engines.
Can I modify the File Path (Windows) regex for my use case?
Yes! Use the interactive tester above to modify the pattern and test with your own data. Common modifications include making it case-insensitive (add 'i' flag), matching globally (add 'g' flag), or adjusting character classes.