Solved

Regex for file naming convention.

Posted on 2008-10-14
5
282 Views
Last Modified: 2012-05-05
Before allowing users to upload files in my application, I need to verify that the
file name follows a strict naming convention.  The first 17 characters must
be formatted as follows:

1.  NN-NNNN(YYYYMMDD).pdf
Ex.  87-2398(20041231).pdf

Please help me with regular expressions that will validate this file naming convention.

Thanks!
0
Comment
Question by:JeffDrummond
  • 3
  • 2
5 Comments
 
LVL 27

Expert Comment

by:ddrudik
ID: 22711294
In general:
^\d\d-\d{4}\(\d{8}\)\.pdf$

If you want a more specific validation performed on the numbers in the pattern (dates etc.) you will need to do that in code.
0
 

Author Comment

by:JeffDrummond
ID: 22711315
Hi ddrudik:

Thanks for the quick response.  I've used your suggestions before to
great effect.

What if I want to leave off the .pdf part of the validation and
just have a regex for this part:  NN-NNNN(YYYYMMDD)  

Thanks.
0
 
LVL 27

Accepted Solution

by:
ddrudik earned 500 total points
ID: 22711438
^\d\d-\d{4}\(\d{8}\)$

Remove the $ if you are passing a string that contains something after the ) in the filename.
0
 

Author Comment

by:JeffDrummond
ID: 22713661
Thanks again.  Right on target.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22713681
Thanks for the question and the points.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question