0

I wrote a regex for validating date in the format MonthName dd, yyyy

The code below works for the date with 2 day digits:

^(January|February|March|April|May|June|July|August|September|October|November|December)\s+\d\d[,]\s+(19|20)\d\d

For date with one digit for day (ex: April 5, 2015) I use:

^(January|February|March|April|May|June|July|August|September|October|November|December)\s+\d[,]\s+(19|20)\d\d

How can I change my regex into a single regex for both options? (April 24, 2015 and April 5, 2015)

Coding Duchess
  • 6,445
  • 20
  • 113
  • 209

2 Answers2

6

The simplest way in line with your code: you just say it should be one or two digits. We can give the character count inside curly braces.

\s+\d{1,2}[,]\s+(19|20)\d{2}

Or more elaborate one as in this answer

Community
  • 1
  • 1
lanenok
  • 2,699
  • 17
  • 24
1

Your complete regex may look like this:

/^(January|February|March|April|May|June|July|August|September|October|November|December)\s+(\d{1,2}),?\s+((?:19|20)\d{2})$/i

Will match:

February 2 1976
August 11 2011
november 19 1966
march 11, 1945

Demo


Regex Explanation:

/^(January|February|March|April|May|June|July|August|September|October|November|December)\s+(\d{1,2}),?\s+((?:19|20)\d{2})$/gmi

i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z])

^ assert position at start of a line
1st Capturing group (January|February|March|April|May|June|July|August|September|October|November|December)
    1st Alternative: January
        January matches the characters January literally (case insensitive)
    2nd Alternative: February
        February matches the characters February literally (case insensitive)
    3rd Alternative: March
        March matches the characters March literally (case insensitive)
    4th Alternative: April
        April matches the characters April literally (case insensitive)
    5th Alternative: May
        May matches the characters May literally (case insensitive)
    6th Alternative: June
        June matches the characters June literally (case insensitive)
    7th Alternative: July
        July matches the characters July literally (case insensitive)
    8th Alternative: August
        August matches the characters August literally (case insensitive)
    9th Alternative: September
        September matches the characters September literally (case insensitive)
    10th Alternative: October
        October matches the characters October literally (case insensitive)
    11th Alternative: November
        November matches the characters November literally (case insensitive)
    12th Alternative: December
        December matches the characters December literally (case insensitive)
\s+ match any white space character [\r\n\t\f ]
    Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
2nd Capturing group (\d{1,2})
    \d{1,2} match a digit [0-9]
        Quantifier: {1,2} Between 1 and 2 times, as many times as possible, giving back as needed [greedy]
,? matches the character , literally
    Quantifier: ? Between zero and one time, as many times as possible, giving back as needed [greedy]
\s+ match any white space character [\r\n\t\f ]
    Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
3rd Capturing group ((?:19|20)\d{2})
    (?:19|20) Non-capturing group
        1st Alternative: 19
            19 matches the characters 19 literally
        2nd Alternative: 20
            20 matches the characters 20 literally
    \d{2} match a digit [0-9]
        Quantifier: {2} Exactly 2 times
$ assert position at end of a line
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268