正则表达式 [英] Regex for date

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

问题描述

  26FEB2009 
2009年7月30日$ b $什么是正则表达式? b 2008年3月27日
29/05/2008
2009年8月27日

应该是正则表达式?



我有正则表达式与 2009年2月26日 26 FEB 2009 而不是 26FEB2009 。所以如果有人知道,请更新它。

 (?:^ | [^ \d\w:]) (?'day'\d {1,2})(?: - ?st\s + |  - ?th\s + |  - ?rd\s + |  - ?nd\s + |  -  | \s + )uaryㄧ))))))))))))))))))))))))))))))))))))))))))))))) (?:'?(?'year'\d {2})|(?'year'\dd {4}))(?= $ | [^ \d\w])

日期26FEB2009是其他字符串的子字符串,如 FUTIDX 26FEB2009 NIFTY 0 ,并从html页面解析,所以我不能设置空格或分隔符。



编辑
示例:

  07.11.2008 
2008年1月11日
2008年5月26日
26FEB2009
2008年3月27日


解决方案

如果匹配26 FEB 2009而不是26FEB2009,听起来像你需要做空格和每个日期段之间的分隔符( - 和/)可选。



+ meta characte r指定一个或多个,考虑使用*(零个或多个)为空格。



编辑



什么我的意思是,如果您的正则表达式与空格/分隔符字符匹配日期,但不匹配日期,而没有其中的任何一个,即26FEB2009,那么这听起来像您指定的空格/分隔符是强制的匹配。



这是我快速敲门的东西:

 (\d {1,2})(\ / |  -  | \s *)?((Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec)| \d { 2})(\ / |  -  | \s *)?(\d {4})

您可能需要检查它是否缺少所需的某些功能,但它与您的所有示例相匹配。


What should be the regex for matching date of any format like

 26FEB2009 
 30 Jul 2009
 27 Mar 2008
 29/05/2008
 27 Aug 2009

What should be the regular expression for that ?

I have regex that matches with 26-Feb-2009 and 26 FEB 2009 with but not with 26FEB2009. So if any one know then please update it.

(?:^|[^\d\w:])(?'day'\d{1,2})(?:-?st\s+|-?th\s+|-?rd\s+|-?nd\s+|-|\s+)(?'month'Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[uarychilestmbro]*(?:\s*,?\s*|-)(?:'?(?'year'\d{2})|(?'year'\d{4}))(?=$|[^\d\w])

The date 26FEB2009 is substring of other string like FUTIDX 26FEB2009 NIFTY 0 and parsed from html page, so I can not set the whitespace or delimiter.

EDIT Examples :

07.11.2008
Jan 11 2008
May 26 2008
26FEB2009
27 Mar 2008

解决方案

If it's matching 26 FEB 2009 and not 26FEB2009, sounds like you need to make the whitespace and delimiter character("-" and "/") between each date segment optional.

The + meta character specifies one or more, consider using * (zero or more) for the whitespace.

EDIT

What I meant was, if your regular expression is matching dates with the whitespace/delimiter character, but is not matching the dates without either of them i.e 26FEB2009, then it sounds like you're specifying that the whitespace/delimiter be compulsory for a match.

Here's something I quickly knocked together:

(\d{1,2})(\/|-|\s*)?((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)|\d{2})(\/|-|\s*)?(\d{4})

You might want to check that it's not missing certain features that you want, but it matches all of your examples.

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

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