各种日期格式的正则表达式 [英] regex for various date formats

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

问题描述

我是Regex的新手,我想知道有人可以帮忙吗?我甚至不知道如果正则表达式是我想要做的最好的选择。



我有一个我正在查看的字符串数组。我想查找数组中匹配各种日期格式的所有项目。以下是我正在尝试匹配的示例格式。



2012年4月1日 - 星期几没有领先零的短月名称 - 匹配



2012年4月1日 - 星期日的首个零点短小月名称比赛



2012年4月1日 - 全文月份名称与否一天中领先零 - 比赛



2012年4月1日 - 全文月份名称,每日头一个零 - 比赛



4/1/2012 - 月和日没有领先的零点 - 比赛



04/01/2012 - 月和日比赛中的领先零点



02/29/2012 - 如果年是一个飞跃,允许2/29 - 匹配



13/12/2001 - 1月以外的月份 - 无比赛



02/30/2012 - 一个月内的太多天 - 无比赛



02/29/2011 - 如果不是闰年 - 没有比赛



我已经开发的当前正则表达式。



($ uary $ uary uary:::::::::::::::::::::::::::::: il??emb emb ust ust ust ust ust ust emb emb emb emb emb emb emb emb emb emb emb emb emb emb emb emb emb emb emb emb emb emb emb emb (0?[1-9] | 1 [012])| | Oct(?:ober)?| Nov(?:ember)?| Dec(?:ember)? ,](0?[1-9] | 1 [0-9] | 2 [0-9] | 3 [01])$ ​​b $ b

我可以开发正则表达式来检测每种格式的各个部分,但将它们链接在一起是为了使我只能得到一个匹配。目前它匹配两次(月,月+日)。



谢谢



jlimited

解决方案

为什么要在一个简单的正则表达式中完成所有操作?听起来更复杂,应该是。此外,想想未来会添加新格式吗?这不是直截了当我宁愿将类似的模式分组成一个单一的正则表达式,例如2012年4月1日,2012年4月1日,然后04/01/2012,02/30/2012。无论如何,我想在某个时候,你需要将日期的不同组成部分处理成一个共同的代表。为此,您可能需要专门的代码。



这不是您想要的答案,但作为一般原则,总是试图将一个大问题变成更小的



欢呼,



Jose


I am newer to Regex and I am wondering if someone can help. I am not even sure if Regex is the best option for what I am trying to do.

I have a array of string that I am looking through. I want to find all items in the array that match various date formats. Here are the example formats that I am trying to match on.

Apr 1, 2012 - Short Month Name with No Leading Zero on Day - Match

Apr 01, 2012 - Short Month Name with Leading Zero on Day - Match

April 1, 2012 - Full Text Month Name with No Leading Zero on Day - Match

April 01, 2012 - Full Text Month Name with Leading Zero on Day - Match

4/1/2012 - No Leading Zeros on Month and Day - Match

04/01/2012 - Leading Zeros on Month and Day - Match

02/29/2012 - If Year is a Leap, allow 2/29 - Match

13/12/2001 - Month outside 1-12 - No Match

02/30/2012 - Too Many Days in a Month - No Match

02/29/2011 - If Not a Leap Year - No Match

Current Regex that I have developed.

\b(?:(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May?|Jun(?:e)?|Jul(?:y) |Aug(?:ust)?|Sep(?:t(?:ember)?)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?))|(0?[1-9]|1[012])[- /.,](0?[1-9]|1[0-9]|2[0-9]|3[01])

I can develop regex to detech the various parts of each of these formats, but linking them together is so that I would only get one match. Currently it matches twice (Month, Month + Day). I only want it to match when the entire match is meet.

Thanks

jlimited

解决方案

Why do you want to do it all in one simple regex? That sounds more complicated that it should be. Besides, think of adding new formats in the future, for example? It would not be straightforward. I would rather group similar patterns into a single regular expression, e.g. "Apr 1, 2012", "April 01, 2012" in one group, and then "04/01/2012", "02/30/2012". Anyway, I guess at some point, you will need to process the different components of the date into a common representation. And for that, you will likely need dedicated code.

It is not perhaps the answer you want, but as a general principle, always try to break a large problem into smaller problems you can easily solve.

Cheers,

Jose

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

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