Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›Regex Tester›Regex for Windows file path
Regex pattern · validation

Regex for Windows file path

Regex for Windows file path: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.

Quick answer

Use /^[A-Za-z]:\\(?:[^\\\/:*?"<>|\r\n]+\\?)*$/ as a Windows file path regex. It matches "C:\" and "C:\Users\Jane\file.txt", and rejects "/usr/bin" and "C:/Users". UNC paths (\\server\share) and reserved names (CON, NUL) are not handled.

Last updated · Maintained by FreeAIToolz · How we test

/^[A-Za-z]:\\(?:[^\\\/:*?"<>|\r\n]+\\?)*$/
  • C:\matches
  • C:\Users\Jane\file.txtmatches
  • D:\Projectsmatches
  • /usr/binno match
  • C:/Usersno match
  • C:\file?.txtno match
  • Users\Janeno match
Open in the full Regex Tester (replace, flags, groups) →

How this regex works

PartMeaning
^[A-Za-z]:\\drive letter, colon and backslash
[^\\\/:*?"<>|\r\n]+a folder or file name without forbidden characters
\\?optional backslash between segments

Examples

InputResult
C:\✓ Matches
C:\Users\Jane\file.txt✓ Matches
D:\Projects✓ Matches
/usr/bin✗ No match
C:/Users✗ No match
C:\file?.txt✗ No match
Users\Jane✗ No match

Every example above is checked automatically in JavaScript and Python each time the site is built.

Limitations and edge cases

UNC paths (\\server\share) and reserved names (CON, NUL) are not handled.

When to use this pattern

Desktop app settings, installers and Windows admin tools.

Use it in your language

JavaScript

const re = /^[A-Za-z]:\\(?:[^\\\/:*?"<>|\r\n]+\\?)*$/;
re.test(input); // true or false

Python

import re
pattern = re.compile(r'^[A-Za-z]:\\(?:[^\\\/:*?"<>|\r\n]+\\?)*$')
bool(pattern.search(value))

PHP

$ok = preg_match('~^[A-Za-z]:\\\\(?:[^\\\\\\/:*?"<>|\\r\\n]+\\\\?)*$~', $value) === 1;

Java

Pattern pattern = Pattern.compile("^[A-Za-z]:\\\\(?:[^\\\\\\/:*?\"<>|\\r\\n]+\\\\?)*$");
boolean ok = pattern.matcher(value).find();

Go

re := regexp.MustCompile(`^[A-Za-z]:\\(?:[^\\\/:*?"<>|\r\n]+\\?)*$`)
ok := re.MatchString(value)

More regex patterns

Frequently asked questions

What is the regex for Windows file path?
/^[A-Za-z]:\\(?:[^\\\/:*?"<>|\r\n]+\\?)*$/. UNC paths (\\server\share) and reserved names (CON, NUL) are not handled.
Which languages does this pattern work in?
It is tested in JavaScript and Python and uses syntax that also works in PHP (PCRE) and Java. It also works in Go’s RE2 engine.
Why does the pattern start with ^ and end with $?
They anchor the match to the whole string, so "abcC:\xyz" cannot pass just because it contains a valid part. To search for matches inside longer text, remove ^ and $ (use \b where word boundaries help) and add the g flag.