Solved

Regex for file naming convention

Posted on 2008-06-23
7
346 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
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.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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…

860 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