Regex for finding valid filename

OrangeRind picture OrangeRind · Jun 23, 2009 · Viewed 22.8k times · Source

I want to check whether a string is a file name (name DOT ext) or not.

Name of file cannot contain / ? * : ; { } \

Could you please suggest me the regex expression to use in preg_match()?

Answer

RichieHindle picture RichieHindle · Jun 23, 2009

Here you go:

"[^/?*:;{}\\]+\\.[^/?*:;{}\\]+"

"One or more characters that aren't any of these ones, then a dot, then some more characters that aren't these ones."

(As long as you're sure that the dot is really required - if not, it's simply: "[^/?*:;{}\\]+"