C# YYYY/MM/DD 日期格式正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19352956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:48:58  来源:igfitidea点击:

YYYY/MM/DD date format regular expression

c#regex

提问by Michael

I want to use regular expression for matching these date formats as below in C#.

我想在 C# 中使用正则表达式来匹配这些日期格式,如下所示。

  1. YYYY/MM/DD 2013/11/12
  2. YYYY/M/DD 2013/5/11
  3. YYYY/MM/D 2013/10/5
  4. YYYY/M/D 2013/5/6
  1. YYYY/MM/DD 2013/11/12
  2. YYYY/M/DD 2013/5/11
  3. YYYY/MM/D 2013/10/5
  4. YYYY/M/D 2013/5/6

I have tried some regular expressions but they can't match the 4 date formats. such as

我尝试了一些正则表达式,但它们无法匹配 4 种日期格式。如

^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])

采纳答案by gwillie

check thisto get an idea of the compexity of regex and validating dates. so i would use

检查这个以了解正则表达式和验证日期的复杂性。所以我会用

\d{4}(?:/\d{1,2}){2}

then in c# do whatever to validate the match. while it can be done, you'll be spending a lot of time trying to achieve it, though there is a regex in that post that with a bit of fiddling supposedly will validate dates in regex, but it is a scary looking regex

然后在 c# 中做任何事情来验证匹配。虽然它可以完成,但您将花费大量时间来尝试实现它,尽管该帖子中有一个正则表达式,据称可以通过一些摆弄来验证正则表达式中的日期,但它看起来很可怕

回答by cxw

Try

尝试

^\d{4}[-/.]\d{1,2}[-/.]\d{1,2}$

The curly braces {}give the number allowed. E.g., \d{1,2}means either one or two digits.

花括号{}给出了允许的数字。例如,\d{1,2}表示一位或两位数字。

回答by bansi

You may need more than that to match date. Try this:

您可能需要更多来匹配日期。尝试这个:

(19|20)\d\d([-/.])(0?[1-9]|1[012])(0?[1-9]|[12][0-9]|3[01])

回答by Ajit

((([0-9][0-9][0-9][1-9])|([1-9][0-9][0-9][0-9])|([0-9][1-9][0-9][0-9])|([0-9][0-9][1-9][0-9]))\-((0[13578])|(1[02]))\-((0[1-9])|([12][0-9])|(3[01])))|((([0-9][0-9][0-9][1-9])|([1-9][0-9][0-9][0-9])|([0-9][1-9][0-9][0-9])|([0-9][0-9][1-9][0-9]))\-((0[469])|11)\-((0[1-9])|([12][0-9])|(30)))|(((000[48])|([0-9][0-9](([13579][26])|([2468][048])))|([0-9][1-9][02468][048])|([1-9][0-9][02468][048]))\-02\-((0[1-9])|([12][0-9])))|((([0-9][0-9][0-9][1-9])|([1-9][0-9][0-9][0-9])|([0-9][1-9][0-9][0-9])|([0-9][0-9][1-9][0-9]))\-02\-((0[1-9])|([1][0-9])|([2][0-8])))

This is the regex for yyyy-MM-ddformat.

这是yyyy-MM-dd格式的正则表达式。

You can replace -with \/for yyyy/MM/dd...

您可以替换-\/yyyy/MM/dd...

Tested working perfect..

测试工作完美..

回答by Ahmed Wahbi

Ajit's regex is nearer to perfect but leaks the evaluation of the leap years that end with 12 and 16. Here is the correction to be just perfect

Ajit 的正则表达式更接近完美,但泄漏了以 12 和 16 结尾的闰年的评估。这是完美的修正

((([0-9][0-9][0-9][1-9])|([1-9][0-9][0-9][0-9])|([0-9][1-9][0-9][0-9])|([0-9][0-9][1-9][0-9]))-((0[13578])|(1[02]))-((0[1-9])|([12][0-9])|(3[01])))|((([0-9][0-9][0-9][1-9])|([1-9][0-9][0-9][0-9])|([0-9][1-9][0-9][0-9])|([0-9][0-9][1-9][0-9]))-((0[469])|11)-((0[1-9])|([12][0-9])|(30)))|(((000[48])|([0-9]0-9)|([0-9][1-9][02468][048])|([1-9][0-9][02468][048])|([0-9]0-9)|([0-9][1-9][13579][26])|([1-9][0-9][13579][26]))-02-((0[1-9])|([12][0-9])))|((([0-9][0-9][0-9][1-9])|([1-9][0-9][0-9][0-9])|([0-9][1-9][0-9][0-9])|([0-9][0-9][1-9][0-9]))-02-((0[1-9])|([1][0-9])|([2][0-8])))

回答by Gowtham K

Try this. This accepts all four patterns

尝试这个。这接受所有四种模式

@"\d{4}[- /.]([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])"