[thelist] regex to validate filenames

Chrome admin at chrome.me.uk
Fri Dec 1 11:48:15 CST 2006

My best advice is to get The Regex Coach and test it out with that...
Sometimes needs a little tweaking depending on the language you need to run
the regex in but it's usually spot on

It's all I use now for testing



>-----Original Message-----
>From: thelist-bounces at lists.evolt.org [mailto:thelist-
>bounces at lists.evolt.org] On Behalf Of Sarah Adams
>Sent: 01 December 2006 16:04
>To: thelist
>Subject: [thelist] regex to validate filenames
>I'm writing a regex to validate filenames. I've got it pretty much
>worked out, but it's getting a little unwieldy, so I thought I'd ask for
>suggestions for simplifying it.
>Basically, I want to validate a filename, checking to make sure that it
>is a gif or jpg, and that it has no illegal characters in it. The files
>are stored on a Linux box, so I'm using that as my basis for filenaming
>restrictions. My understanding is that these characters should be
>avoided in filenames on Linux:
>* : \ / < > | " ? NULL (and all non-printing characters; i.e. \n) I also
>want to make sure the filename can't start with whitespace or "."
>(which indicates a hidden file).
>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
>sarah adams
>web developer & programmer
>portfolio: http://sarah.designshift.com
>blog: http://hardedge.ca
>* * Please support the community that supports you.  * *
>For unsubscribe and other options, including the Tip Harvester and
>archives of thelist go to: http://lists.evolt.org Workers of the Web,
>evolt !
>__________ NOD32 1889 (20061129) Information __________
>This message was checked by NOD32 antivirus system.

More information about the thelist mailing list