试图改进日期验证的正则表达式

时间:2015-04-24 16:58:06

标签: regex validation datetime

我写了一个用于以DateName dd,yyyy

格式验证日期的正则表达式

以下代码适用于2天数字的日期:

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

对于每天一位数的日期(例如:2015年4月5日),我使用:

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

如何将我的正则表达式更改为两个选项的单个正则表达式? (2015年4月24日和2015年4月5日)

2 个答案:

答案 0 :(得分:6)

符合您代码的最简单方法:您只需说它应该是一位或两位数。我们可以在花括号内给出字符数。

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

或者更精细的一个,如this answer

答案 1 :(得分:1)

您的完整正则表达式可能如下所示:

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

将匹配:

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

Demo

正则表达式解释:

/^(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