Back to Home

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!

Generate Regex Pattern

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

ComponentDescriptionMatches
[^...]Negated character classMatches any character NOT in the list
\\BackslashNot allowed
/Forward slashNot allowed
:ColonNot allowed
*AsteriskNot allowed
?Question markNot allowed
"Double quoteNot allowed
<Less thanNot allowed
>Greater thanNot allowed
|PipeNot allowed
+QuantifierOne 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.txt
  • document (1).pdf
  • file-name_123.docx
  • test file.txt
  • file.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

  1. No reserved names check: Doesn't prevent reserved names like CON, PRN, AUX, NUL, etc.
  2. No length limit: Doesn't enforce Windows filename length limits (260 chars for full path)
  3. No trailing spaces/dots: Doesn't prevent trailing spaces or dots (invalid in Windows)
  4. Pattern only: This pattern matches valid characters, not full filename validation
  5. 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!

Generate Regex Pattern