Regex for illegal filenames in windows

I am a bit confused by regex syntax. I need to build two separate Regex patterns that detects whether a filename is legal in windows. One is that matches any word except these chars (illegal characters) –

*"< > : " / \ | ? "

And the second pattern is that matches any word except these words (reserved file names) –

PRN, AUX, CLOCK, NUL, CON, COM, LPT

I found combined version of this pattern that looks like this @"^(?!(?:PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d)(?:\..+)?$)[^\x00-\x1F\xA5\\?*:\"";|\/<>]+(?<![\s.])$", but the key thing is that I need to separate these two.

Could anyone help me? Thank you in advance.

Add Comment
1 Answer(s)

There are actually three things this regex checks for.

You can validate any regex here: https://regex101.com/

This will negate all the occurrences of reserved file names in any position of text:

    (?:PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d) 

but to match exact name you need ^ and & which indicate start and end of the text, so this will work for second group:

#1

    ^(?:PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d)$ 

And for the invalid characters, this will exclude all the given characters:

#2

    ^[^\x00-\x1F\xA5\\?*:\"";|\/<>]+$ 

but it still allows ., a. and .a. To exclude those three possibilities you need to:

first add (?!(?:\..+)?$) to beginning to exclude names starting with . (that is \.)

then add (?<![.]) to end to exclude names ending with .

#3

    ^(?!(?:\..+)?$).+(?<![.])$ 

But this will allow many other possibilities with whitespace at the beginning and the end.

At this point you can either trim the text, ignore the invalid names (Windows will trim them) or extend your regex as follows:

adding \x20 excludes a and a. but still allows a and .a

    ^(?!(?:\..+)?$).+(?<![\x20.])$ 

adding (?!(?:\x20+.+)?$) excludes starting whitespaces:

#3

    ^(?!(?:\x20+.+)?$)(?!(?:\..+)?$).+(?<![\x20.])$ 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.