Solved

Regex for file naming convention

Posted on 2008-06-23
7
345 Views
Last Modified: 2011-09-20
Before allowing users to upload files in my application, I need to verify that the
file name follows one of two strict naming conventions:

1.  NN-NNNN(MMDDYYYY).pdf
Ex.  87-2398(01312004).pdf

2.  NN-NNNN(MMDDYYYY)UPD.pdf
Ex.  98-8675(12311999)UPD.pdf

Please help me with regular expressions that will validate these file names.

Thanks!
0
Comment
Question by:JeffDrummond
7 Comments
 
LVL 27

Accepted Solution

by:
ddrudik earned 500 total points
ID: 21846535
^\d\d-\d{4}\(\d{8}\)(?:UPD)?\.pdf$
0
 

Author Comment

by:JeffDrummond
ID: 21846717
Hi ddrudik:

Thanks for the super-fast response.  One more question...I got the date format wrong
in my question.  It should be YYYYMMDD instead of MMDDYYYY.  Can you modify
the expression for enforce that date format.

Thanks.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 21846840
My expression doesn't take into account number of days of the month per month or year ranges so no modification is required for your change.

To validate dates at that level a more complex pattern based on actual months/years would be required for that, my pattern just requires any number that is 8 digits long.  

Regex is not the easiest method to validate dates but if you need to use regex you will need to define what years you want to validate for and then you will need to take into account days per month in those years.  Personally if you needed to actually validate the date entered I would recommend you use a function within whatever programming language you are using the regex in, usually such functions are available.

If you are curious to see how complex such a pattern can get, here's a reference pattern for a similar task:
http://regexlib.com/REDetails.aspx?regexp_id=279
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 9

Expert Comment

by:ghostdog74
ID: 21848135
what language are you using ? there's no need for regular expression.

1. Get the index of "("
2) Get the index of ")"
3) Do a substring between these 2 indexes to get the date
4) Do a substring to get MM, DD and YYYY
5) For MM, do a check for 1 to 12
6) For DD, do a check for 1 to 30,31 ( might have to code extra check for leap years and mths with 30 days etc)
7) For YYYY, depending on your criteria. eg less than 2011??? etc
0
 
LVL 10

Expert Comment

by:margajet24
ID: 21856520
if there is pattern, you can use regular expression..

consider when the format is changed, do you need to change the whole code or just a simple expression?..

it is just my opinion..
0
 

Author Closing Comment

by:JeffDrummond
ID: 31469756
This was very useful.  Thanks.
0
 
LVL 27

Expert Comment

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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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…
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…

786 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