Regex 尝试改进用于日期验证的正则表达式
我编写了一个用于验证日期的正则表达式,格式为MonthName dd,yyyy 以下代码适用于具有2个日期数字的日期:Regex 尝试改进用于日期验证的正则表达式,regex,validation,datetime,Regex,Validation,Datetime,我编写了一个用于验证日期的正则表达式,格式为MonthName 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日),我使用: 如何将两个选项的正则表达式更改为单个正则表达式?(2015年4月24日和2015年4月5日
^(January|February|March|April|May|June|July|August|September|October|November|December)\s+\d\d[,]\s+(19|20)\d\d
对于日期为一位数的日期(例如:2015年4月5日),我使用:
如何将两个选项的正则表达式更改为单个正则表达式?(2015年4月24日和2015年4月5日)符合您的代码的最简单方式:您只需说它应该是一个或两个数字。我们可以在花括号内给出字符数
\s+\d{1,2}[,]\s+(19|20)\d{2}
或者更详细的一个,如您的完整正则表达式可能如下所示:
/^(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
正则表达式解释:
/^(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
你不应该用正则表达式解析日期…它还会匹配1976年2月2日和2011年8月11日吗?还有一种方法可以使月份名称不区分大小写吗?谢谢,只是在末尾添加了
/i
。你用哪种语言?我将更新我的答案,使其不区分大小写。要匹配,
您需要将逗号设置为可选的,?
我将再更新一次,如果可能的话,下次请详细说明您问题的所有内容,这对两个问题都比较容易。如果我的问题对您有帮助,请查看,请考虑接受它作为正确的答案。Tks!
/^(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