正则表达式用于匹配多种日期格式 [英] Regex for matching multiple date formats

查看:602
本文介绍了正则表达式用于匹配多种日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

匹配以下任何格式的日期的正则表达式应该是什么:

What should be the regex for matching date of any format like:

26FEB2009
31DEC2009

27 Mar 2008
30 Jul 2009

26-Feb-2009
27-Aug-2009

29/05/2008

07.11.2008

Jan 11 2008
May 26 2008

正则表达式应该是什么?

What should be the regular expression for that?

我的正则表达式与 2009年2月26日 2009年2月26日匹配,但与 26FEB2009 不匹配.因此,如果有人知道,请对其进行更新.

I have regex that matches with 26-Feb-2009 and 26 FEB 2009 with but not with 26FEB2009. So if any one know then please update it.

(?:^|[^\d\w:])(?'day'\d{1,2})(?:-?st\s+|-?th\s+|-?rd\s+|-?nd\s+|-|\s+)(?'month'Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[uarychilestmbro]*(?:\s*,?\s*|-)(?:'?(?'year'\d{2})|(?'year'\d{4}))(?=$|[^\d\w])

日期26FEB2009是其他字符串的子字符串,例如 FUTIDX 26FEB2009 NIFTY 0 ,并且是从html页解析的,因此我无法设置空格或定界符.

The date 26FEB2009 is substring of other string like FUTIDX 26FEB2009 NIFTY 0 and parsed from html page, so I can not set the whitespace or delimiter.

推荐答案

如果它与2009年2月26日匹配,而与26FEB2009不匹配,听起来您需要在每个日期之间使用空格和定界符(-"和"/")段可选.

If it's matching 26 FEB 2009 and not 26FEB2009, sounds like you need to make the whitespace and delimiter character("-" and "/") between each date segment optional.

+元字符指定一个或多个,请考虑对空格使用*(零个或多个).

The + meta character specifies one or more, consider using * (zero or more) for the whitespace.

编辑

我的意思是,如果您的正则表达式将日期与空格/分隔符匹配,但不与没有日期的日期匹配,即26FEB2009,则听起来您正在指定必须使用空格/分隔符进行比赛.

What I meant was, if your regular expression is matching dates with the whitespace/delimiter character, but is not matching the dates without either of them i.e 26FEB2009, then it sounds like you're specifying that the whitespace/delimiter be compulsory for a match.

这是我很快就把它们撞在一起的东西:

Here's something I quickly knocked together:

(\d{1,2})(\/|-|\s*)?((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)|\d{2})(\/|-|\s*)?(\d{4})

您可能要检查它是否没有缺少某些所需的功能,但它与所有示例都匹配.

You might want to check that it's not missing certain features that you want, but it matches all of your examples.

这篇关于正则表达式用于匹配多种日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆