Windows Filename Validation Regex (Invalid Characters)
• CronOS Team
regexfilenamevalidationtutorialwindows
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!
Windows Filename Validation Regex (Invalid Characters)
Validate Windows filenames by ensuring they don't contain invalid characters using a negated character class regex pattern.
Pattern Breakdown
regex
[^\\/:*?"<>|]+
Components
| Component | Description | Matches |
|---|---|---|
[^...] | Negated character class | Matches any character NOT in the list |
\\ | Backslash | Not allowed |
/ | Forward slash | Not allowed |
: | Colon | Not allowed |
* | Asterisk | Not allowed |
? | Question mark | Not allowed |
" | Double quote | Not allowed |
< | Less than | Not allowed |
> | Greater than | Not allowed |
| | Pipe | Not allowed |
+ | Quantifier | One or more valid characters |
Character Classes
[^\\/:*?"<>|]- Negated character class: matches any character EXCEPT the listed invalid ones+- Quantifier: one or more occurrences
Examples
Valid:
myfile.txtdocument (1).pdffile-name_123.docxtest file.txtfile.name.txt
Invalid:
file/name.txt(contains forward slash)file:name.txt(contains colon)file*name.txt(contains asterisk)file?name.txt(contains question mark)file<name>.txt(contains angle brackets)file|name.txt(contains pipe)file\name.txt(contains backslash)file"name.txt(contains double quote)
Implementation
JavaScript
javascript
const filenameRegex = /[^\\/:*?"<>|]+/;
filenameRegex.test('myfile.txt'); // true
filenameRegex.test('document (1).pdf'); // true
filenameRegex.test('file/name.txt'); // false (contains invalid char)
filenameRegex.test('file:name.txt'); // false (contains colon)
// For full filename validation (with anchors)
const fullFilenameRegex = /^[^\\/:*?"<>|]+$/;
fullFilenameRegex.test('myfile.txt'); // true
fullFilenameRegex.test('file/name.txt'); // false
Python
python
import re
filename_regex = r'[^\\/:*?"<>|]+'
bool(re.match(filename_regex, 'myfile.txt')) # True
bool(re.match(filename_regex, 'document (1).pdf')) # True
bool(re.match(filename_regex, 'file/name.txt')) # False (contains invalid char)
# For full filename validation
full_filename_regex = r'^[^\\/:*?"<>|]+$'
bool(re.match(full_filename_regex, 'myfile.txt')) # True
Go
go
filenameRegex := regexp.MustCompile(`[^\\/:*?"<>|]+`)
filenameRegex.MatchString("myfile.txt") // true
filenameRegex.MatchString("document (1).pdf") // true
filenameRegex.MatchString("file/name.txt") // false (contains invalid char)
// For full filename validation
fullFilenameRegex := regexp.MustCompile(`^[^\\/:*?"<>|]+$`)
fullFilenameRegex.MatchString("myfile.txt") // true
Limitations
- No reserved names check: Doesn't prevent reserved names like
CON,PRN,AUX,NUL, etc. - No length limit: Doesn't enforce Windows filename length limits (260 chars for full path)
- No trailing spaces/dots: Doesn't prevent trailing spaces or dots (invalid in Windows)
- Pattern only: This pattern matches valid characters, not full filename validation
- No path validation: Only validates filename part, not full path
When to Use
- Filename sanitization
- File upload validation
- Preventing invalid characters in filenames
- When you need to filter out Windows-invalid characters
- Input validation for file operations
For production, consider:
- Adding anchors (
^...$) for full filename validation - Checking for reserved Windows filenames (CON, PRN, AUX, NUL, COM1-9, LPT1-9)
- Preventing trailing spaces and dots
- Enforcing length limits (260 chars for full path, 255 for filename)
- Using OS-specific filename validation libraries
- Combining with path validation if needed
Need to generate a regex pattern?
Use CronOS to generate any regex pattern you wish with natural language. Simply describe what you need, and we'll create the perfect regex pattern for you. It's completely free!