Solved

Regex for file naming convention

Posted on 2008-06-23
7
343 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
validating special characters 5 41
Regex in powershell (part 2) 6 80
Need Help to Improve the regular expression 3 48
Help with REGEX with 2 different strings 14 56
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…

895 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

18 Experts available now in Live!

Get 1:1 Help Now