Regular Expression for File Name with No Space and a Single 3 Character Extension
JiaPei Jen
Ranch Hand
Joined: Nov 19, 2000
Posts: 1309
posted
0
If we require a file name submitted by users must not have space and have a single 3 character extension (for example: .doc, .txt, .pdf)
I think the 3 characters extension may be something similar to ^\d{3}\d*$ (I can be wrong). And how do we set the 'no space' constraint?
David Harkness
Ranch Hand
Joined: Aug 07, 2003
Posts: 1646
posted
0
You'll need to be more specific. Do you want to allow digits in the extension? What about non-alphanumeric characters in the filename? In any case, here's one that allows any non-space character in the base name and any alphanumeric (letter or number) character in the extension:Note that this allows a newline (\n) and other symbol characters for the base name. You may want to limit this a bit to something like
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
posted
0
David,
Did you miss the ^ towards the beginning of your second regex?
Originally posted by Layne Lund: Did you miss the ^ towards the beginning of your second regex?
No, the first regex excludes space while the second one includes a specific set of characters: a-z A-Z 0-9 - _ , ! ( and ). It all depends on how restrictive JiaPei wants to be with the two parts of the filename. For example, must it match Unix or Windows naming restrictions?
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
posted
0
I just stumbled across RegEx Coach the other day. It's a neat interactive way to play with expressions and see how they work. It won't write them for you but it will surely help you understand them. Anybody else have favorite tools in this space?
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi