[thelist] regex to validate filenames

Ken Snyder ksnyder at coremr.com
Fri Dec 1 11:47:23 CST 2006


Sarah Adams wrote:
> So here's what I'm thinking:
> - start of string:
>   ^
> - one allowable character, except not whitespace or ".":
>   [\w`~!@#$%^&()\-=+[\]{};',]
> - 0 or more allowable characters:
>   [\w`~!@#$%^&()\-=+[\]{};', \t.]*
> - a dot:
>   \.
> - an allowed file extension:
>   (GIF|JPG)
> - end of string:
>   $
>
> So I have:
> ^[\w`~!@#$%^&()\-=+[\]{};',][\w`~!@#$%^&()\-=+[\]{};', \t.]*\.(GIF|JPG)$
>
> I tried to figure out a shortcut for specifying "one or more allowed
> characters with the first not being whitespace or '.'", so that I
> wouldn't have to repeat the list of allowed characters, but I had no luck.
>
> Suggestions?
>
>   
Not sure what is wrong with a long regex.  You can of course do
[^\s\.]
to mean not whitespace and not dot, but you probably want to stick to 
how you are doing it because there are a lot of invalid characters that 
are not whitespace and not dot.  And it is usually better to specify all 
that is allowed rather than trying to enumerate all that isn't allowed.
One suggestion, you might change (GIF|JPG) to (GIF|JPE?G) to allow .jpeg 
files as well.

--Ken





More information about the thelist mailing list