[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

Regex for file naming convention

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
JeffDrummond
Asked:
JeffDrummond
1 Solution
 
ddrudikCommented:
^\d\d-\d{4}\(\d{8}\)(?:UPD)?\.pdf$
0
 
JeffDrummondAuthor Commented:
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
 
ddrudikCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
ghostdog74Commented:
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
 
margajet24Commented:
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
 
JeffDrummondAuthor Commented:
This was very useful.  Thanks.
0
 
ddrudikCommented:
Thanks for the question and the points.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now