Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular Expression for File Name with No Space and a Single 3 Character Extension

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,

Did you miss the ^ towards the beginning of your second regex?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, but I want to mention, that ~ is an often used filename-part (for backups), and valid on dos and linux.

and I don't like '.jav', '.htm' and '.cla' - files.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic