Solved

Regex for file naming convention.

Posted on 2008-10-14
5
278 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
Comment Utility
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
Comment Utility
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
Comment Utility
^\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
Comment Utility
Thanks again.  Right on target.
0
 
LVL 27

Expert Comment

by:ddrudik
Comment Utility
Thanks for the question and the points.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now