aspose file tools*
The moose likes Java in General and the fly likes Regular Expression for File Name with No Space and a Single 3 Character Extension Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regular Expression for File Name with No Space and a Single 3 Character Extension" Watch "Regular Expression for File Name with No Space and a Single 3 Character Extension" New topic
Author

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

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
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
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
David,

Did you miss the ^ towards the beginning of your second regex?


Java API Documentation
The Java Tutorial
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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
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
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

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.


http://home.arcor.de/hirnstrom/bewerbung
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regular Expression for File Name with No Space and a Single 3 Character Extension