Regex for Windows File Path
Regex Pattern
^[a-zA-Z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$Windows absolute file path (C:\folder\file.txt)
Quick Answer
The regex pattern for windows file path is `^[a-zA-Z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$`. Windows absolute file path (C:\folder\file.txt). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.
Test Examples
| Input | Result |
|---|---|
| C:\Users\file.txt | ✓ Matches |
| D:\Program Files\app.exe | ✓ Matches |
| E:\ | ✓ Matches |
| /unix/path | ✗ No match |
| C: | ✗ No match |
| C:\invalid*name | ✗ No match |
Code Examples
javascript
const regex = /^[a-zA-Z]:\\(?:[^\\\/:*?"<>|\r\n]+\\)*[^\\\/:*?"<>|\r\n]*$/; const isValid = regex.test(value);
python
import re
pattern = r'^[a-zA-Z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$'
if re.match(pattern, value):
print("valid")ruby
pattern = /^[a-zA-Z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*[^\\/:*?"<>|\r\n]*$/ if value =~ pattern puts "valid" end
php
if (preg_match('/^[a-zA-Z]:\\(?:[^\\\/:*?"<>|\r\n]+\\)*[^\\\/:*?"<>|\r\n]*$/', $value)) {
echo "valid";
}java
String pattern = "^[a-zA-Z]:\\\\(?:[^\\\\/:*?"<>|\\r\\n]+\\\\)*[^\\\\/:*?"<>|\\r\\n]*$"; boolean isValid = value.matches(pattern);
Frequently Asked Questions
Related Regex Patterns
File Extension
File extension at the end of a filename
Image File Extension
Common image file extensions
Video File Extension
Common video file extensions
Absolute Unix Path
Absolute filesystem path starting with /
Unix File Path
Unix absolute file path (must have at least one path component)
Filename with Extension
Filename with at least one extension