0

I am looking for a regex to validate the following format AaaD-DDMMYY-HH-MM although the expression below works on all tests online http://www.freeformatter.com/regex-tester.html it does not work in the front end of the application which allows us to enter a preset regex.

We are trying to use regex to allow these date ranges 010120 to 311299 (JAN 01 1920 to DEC 31 1999).

Please assist with a better REGEX

sample input to be matched is aadd-111199-01-01

this input aadd-111100-01-01 should not be matched

^(([a-zA-Z]{4}-)+(0[1-9]|1[0-9]|2[0-9]|3[0-1])+(0[1-9]|1[0-2])+(([2-9][1-9])-)+(\d{2}-)+(\d{2}))$

The front end application I am trying to update the regex was written in java

5
  • What's some sample input that you want it to match and not match? What is AaaD? Are the months formatted as letters (JAN, FEB, ..., DEC) or numbers (01, 02, ..., 12)? Do you want it to account for the fact that months have different numbers of days? Do you care if September 31, for example, passes the regex?
    – pzp
    Nov 30, 2015 at 23:18
  • Some examples of strings you want to match and not match please? Dec 1, 2015 at 0:09
  • @GeorgeAppiahSarfo What does aadd symbolize? Do you literally want to match those characters? Do they symbolize any letters? Should it be only lowercase letters?
    – pzp
    Dec 1, 2015 at 1:38
  • It is a mix of lower and uppercase letters Dec 1, 2015 at 16:15
  • @JustAnotherCoder sample input to be matched is aadd-111199-01-01 this input aadd-111100-01-01 should not be matched Dec 1, 2015 at 16:16

1 Answer 1

0

Here's a slightly cleaned up version, although I'm not sure if it will fix your problem of the RegEx not working in the front-end of your application:

^(([a-zA-Z]{4})-(0[1-9]|1[0-9]|2[0-9]|3[0-1])(0[1-9]|1[0-2])([2-9][1-9])-(\d{2})-(\d{2}))$

You should not assign repeaters after a capturing group (you had a '+' after each). When you do that, if the content for the group is repeated, only the last iteration is captured in the backreference (in the case of your string I think repetition was unlikely, but you had assumed the capturing group would need to exist "at least once", but since a capturing group only captures the content it surrounds, it in itself doesn't imply character or character group that would need a quantifier). Repetitions (such as '+' or '*' or {n}) make more sense inside a capturing group, if needed.

You also had the hyphens within the capturing groups, which is not likely (?) what you wanted as it makes the parsing of the backreference content more challenging. But other than that the given RegEx matches your example string with PCRE RegEx. What is your front-end application created with? Does it use PCRE, or some other RegEx flavor (here is a good reference for various RegEx flavors)? I also recommend RegEx Buddy tool if you do a lot of work with Regular Expressions.

Also, like @pzp mentioned, with RegEx it's challenging to detect the correct number of dates for each month. If you want validation of the given date, then I would recommend some library to check the date after it has passed the format validation with the RegEx. For example, if you're using JavaScript in the front end of the application, you could check out Moment.js or Datejs.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.