Solved

Regex for file naming convention

Posted on 2008-06-23
7
342 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
Comment Utility
^\d\d-\d{4}\(\d{8}\)(?:UPD)?\.pdf$
0
 

Author Comment

by:JeffDrummond
Comment Utility
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
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 9

Expert Comment

by:ghostdog74
Comment Utility
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
Comment Utility
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
Comment Utility
This was very useful.  Thanks.
0
 
LVL 27

Expert Comment

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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

762 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

6 Experts available now in Live!

Get 1:1 Help Now