正则表达式匹配日期,如月份名称日逗号和年份 [英] Regex to match date like month name day comma and year

查看:77
本文介绍了正则表达式匹配日期,如月份名称日逗号和年份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何匹配2014 年 10 月 21 日"或2014 年 10 月 21 日"这样的日期

I would like to know how to match a date like this one "Oct 21, 2014" or "October 21, 2014"

到目前为止我所做的是 \b(?:Jan?|?:Feb?|?:Mar?|?:Apr?|?:May?|?:Jun?|?:Jul?|?:Aug?|?:Sep?|?:Oct?|?:Nov?|?:Dec?) [0-9]{1,2}[,] (?:19[7-9]\d|2\d{3})(?=\D|$) 但这并没有让我去任何地方

What I have done so far is \b(?:Jan?|?:Feb?|?:Mar?|?:Apr?|?:May?|?:Jun?|?:Jul?|?:Aug?|?:Sep?|?:Oct?|?:Nov?|?:Dec?) [0-9]{1,2}[,] (?:19[7-9]\d|2\d{3})(?=\D|$) but that doesn't get me anywhere

  • 简而言之,我需要匹配的字符串为:Month[space]Day[comma][space]Year"我不关心闰年和月份的天数应该是 1 到 31 之间的任何东西,没有前导 0
  • 我需要这个正则表达式来处理 python

推荐答案

可能满足您的需求.

但是请记住,您将需要更复杂的验证,例如验证特定月份的天数(例如,2 月最多只有 28 天(在 bissext 年份中为 29 天,依此类推)

Keep in mind however that you will need more sophisticated validations such as validating the number of days for a specific month (say, February can have up to 28 days only (29 in bissext years), and so on)

(Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)\s+(\d{1,2})\s+(\d{4})

试试看这里.

同样,这绝对是一个非常简单的正则表达式,您必须有许多更好的解决方案,但也许这足以满足您的需求,我不知道.

Again, this is definitely a very simple regex and you must have many better solutions out there, but perhaps this may be enough to your needs, I do not know.

这篇关于正则表达式匹配日期,如月份名称日逗号和年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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