用于捕获不同日期格式的正则表达式 [英] Regex for capturing different date formats

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

问题描述

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

  02 APR 
APR 02
2 APR
APR 2
第二APR
APR 2nd
4月2日
4月2日
APR 12日
4月12日
12月12日
4月13-16日
4月16日至16日
APR 13-16
13-16 APR
4月13日至16日
3月13日至16日
APR 13日至16日
13日至16日APR

我已经尝试过许多方法,但是不能理解,因为我是一个
newbie to regex。 / p>

最接近我使用的是:

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

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



第13至第16 APR



13〜16四月



13/16 APR



我试过使用以下:

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

没有空间。
有没有一种捕获所有格式的方式,并将' - ','/','〜'和输出/写入的日期分割成单一的标准格式?

 (第1组日期)-Month(第2组日期) - 例如:13-Apr 16-Apr 

欣赏您的善意建议和意见。

解决方案

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

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

请参阅这里的详细信息: Regex101






也许这是过度的,但我想出了这个正则表达式,将匹配任何一个月:

 (?:1月| JAN | 2月| FEB | 3月| MAR | 4月| 5月| 5月| 6月| 6月| 7月| JUL | 8月| AUG | 9月| SEP | 10月| OCT | 11月| NOV | 12月| DEC)\ * \d +(?:[nr] d | th | st) ?(?: -  \d +(?:[nr] d | th | st)?)?| \d +(?:[nr] d | th | st) ?(?: -  \d +(?:[nr] d | th | st)?)?\ *(?:1月| JAN |二月| FEB |三月| MAR |四月| APR |五月|五月|六月| JUN | 7月| JUL | 8月| AUG | 9月| SEP | 10月| OCT | 11月| NOV | 12月| DEC)

无法读取,请在此处查看要获取的详细信息: Regex101






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

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

查看详情: Regex101

它匹配每个monthes,它使用较少的步骤(更有效率)

但是,你需要确保你不区分大小写


I'm tasked to capture date for itineraries in email message, but the dates given were all in different formats, I guess I need help to find out if there's any way to capture the following formats:

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

I've tried numerous ways but just could not understand or fathom as I'm a newbie to regex.

The closest I could get was using this:

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

EDIT- Found out that i`ve missed some more formats.

13th - 16th APR

13~16 April

13/16 APR

I`ve tried using the following:

(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)?)

Could either capture dates with space or without space. Is there a way to capture all formats, and split the dates with '-', '/','~' and output/write into a single standardize format?

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

Appreciate for your kind suggestions and comments.

解决方案

I came up with this Regex:

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

See details here: Regex101


Maybe it's overkill, but I came up with this regex that will match with any month:

(?: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)

Unreadable, check here if you want details: Regex101


Improved version using Wiktor Stribiżew's trick:

(?: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)?)

See details here: Regex101
It matches every monthes, it uses less steps (more efficient)
BUT, you need to make sure you're case insensitive

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

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