我的任务是在电子邮件中捕获行程的日期,但是给出的日期都是不同的格式,我想我需要帮助才能找出是否有任何方法可以捕获以下格式:
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*
编辑 – 发现我错过了更多的格式.
4月13日至16日
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
感谢您的善意建议和意见.
解决方法
我想出了这个正则表达式:
(?: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,它使用更少的步骤(更有效)但是,你需要确保你不区分大小写