用于捕获不同日期格式的正则表达式

时间:2016-04-18 09:06:27

标签: regex date

我的任务是在电子邮件中捕获行程的日期,但是给出的日期都是不同的格式,我想我需要帮助才能找出是否有任何方法可以捕获以下格式:

02 APR
APR 02
2 APR
APR 2
2nd APR
APR 2nd
2nd April
April 2nd
APR 12th
April 12th
12th April
April 13-16
13-16 April
APR 13-16
13-16 APR
April 13th-16th
13th-16th April
APR 13th-16th
13th-16th APR

我尝试了很多方法,但是因为我不能理解或理解 新手到正则表达式。

我能得到的最接近的是:

(\d*)-(\d*) APR|April \d*\d*

编辑 - 发现我错过了更多格式。

13-16 - 4月APR

4月13日至16日

13/16 APR

我尝试过使用以下内容:

(Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)\ *\d+(?:[nr]d|th|st)?(?: * \d+(?:[nr]d|th|st)?)?|\d+(?:[nr]d|th|st)?(?: . \d+(?:[nr]d|th|st)?)?\ *(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)

可以用空格或没有空格捕获日期。 有没有办法捕获所有格式,并将日期分为' - ','/','〜'和输出/写入一个标准化格式?

(Group 1 Date)-Month (Group 2 Date)-Month eg: 13-Apr 16-Apr

感谢您的善意建议和意见。

3 个答案:

答案 0 :(得分:2)

您需要考虑可选值。以下是与您的示例输入相匹配的增强版本:

/(\d+)(?:st|[nr]d|th)?-?(\d*)(?:st|[nr]d|th)?\s*Apr(?:il)?|Apr(?:il)?\s*(\d+)(?:st|[nr]d|th)?-?(\d*)(?:st|[nr]d|th)?/i

请参阅regex demo请注意,您需要使用不区分大小写的修饰符来匹配April 的任何变体)

基本上,有两个匹配April和日期范围的替代方案:

  • (\d+)(?:st|[nr]d|th)?-?(\d*)(?:st|[nr]d|th)?\s*Apr(?:il)? - 1+位数后跟可选的stndrdth,后跟可选的连字符,然后是0+数字,后跟可选的st等,然后是0 +空格,然后是AprApril(由于/i修饰符而不区分大小写)
  • | - 或
  • Apr(?:il)?\s*(\d+)(?:st|[nr]d|th)?-?(\d*)(?:st|[nr]d|th)? - 与上述相同但已交换。

答案 1 :(得分:1)

我想出了这个正则表达式:

(?:APR|April)\ *\d+(?:[nr]d|th|st)?(?:-\d+(?:[nr]d|th|st)?)?|\d+(?:[nr]d|th|st)?(?:-\d+(?:[nr]d|th|st)?)?\ *(?:APR|April)

请在此处查看详细信息:Regex101

也许它有点矫枉过正,但我​​想出了这个与任何月份相匹配的正则表达式:

(?:January|JAN|February|FEB|March|MAR|April|APR|May|MAY|June|JUN|July|JUL|August|AUG|September|SEP|October|OCT|November|NOV|December|DEC)\ *\d+(?:[nr]d|th|st)?(?:-\d+(?:[nr]d|th|st)?)?|\d+(?:[nr]d|th|st)?(?:-\d+(?:[nr]d|th|st)?)?\ *(?:January|JAN|February|FEB|March|MAR|April|APR|May|MAY|June|JUN|July|JUL|August|AUG|September|SEP|October|OCT|November|NOV|December|DEC)

不可读,如果您需要详细信息,请点击此处:Regex101

使用WiktorStribiżew的技巧改进版本:

(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)\ *\d+(?:[nr]d|th|st)?(?:-\d+(?:[nr]d|th|st)?)?|\d+(?:[nr]d|th|st)?(?:-\d+(?:[nr]d|th|st)?)?\ *(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)

详情请见Regex101
它匹配每个monthes,它使用更少的步骤(更有效)
,您需要确保不区分大小写

答案 2 :(得分:0)

我想出了这个:

(\d+(?:th|st|[nr]d)?(?:-\d+(?:th|st|[nr]d)?)?\s*(?:APR|April))|((?:APR|April)\s*\d+(?:th|st|[nr]d)?(?:-\d+(?:th|st|[nr]d)?)?)

Live Demo