[英]Regular expression to extract Date value specified in word from the string in Java
I have the Date column which contains the following sample values 我有日期列,其中包含以下示例值
I want to extract the dates (in BOLD) in the given string.Can I get a regular expression which would detect this date specified in words. 我想提取给定字符串中的日期(以粗体显示),我可以得到一个正则表达式来检测单词中指定的日期。
Thanks!! 谢谢!!
I guess it depends how strict you need the expression to be. 我想这取决于您需要表达的严格程度。 This one will work for all your examples:
这将适用于您的所有示例:
/(January|February|March|April|May|June?|July|August|September|October|November|December)\\s(\\d\\d?).+?(\\d\\d\\d\\d)/
But there is no enforcement of the st , nd , rd , th rules. 但是没有执行st , nd , rd , th规则。
Nor is there an enforcement on the comma separating the day from the year. 逗号也没有强制执行,将日期与年份分开。
And there is special case for a shortened June (for your example 5 there is an optional e
for June), but no account taken for other shortened month names. 还有一个特殊情况,即六月的缩短(例如,示例5中有一个可选的6月的
e
),但没有考虑其他缩短的月份的名称。
Sample output from Firebug: Firebug的示例输出:
>>> /(January|February|March|April|May|June?|July|August|September|October|November|December)\s(\d\d?).+?(\d\d\d\d)/.exec(s1)
["June 25, 2010", "June", "25", "2010"]
>>> /(January|February|March|April|May|June?|July|August|September|October|November|December)\s(\d\d?).+?(\d\d\d\d)/.exec(s2)
["March 14, 2011", "March", "14", "2011"]
>>> /(January|February|March|April|May|June?|July|August|September|October|November|December)\s(\d\d?).+?(\d\d\d\d)/.exec(s3)
["April 15, 2011", "April", "15", "2011"]
>>> /(January|February|March|April|May|June?|July|August|September|October|November|December)\s(\d\d?).+?(\d\d\d\d)/.exec(s4)
["March 31st, 2011", "March", "31", "2011"]
>>> /(January|February|March|April|May|June?|July|August|September|October|November|December)\s(\d\d?).+?(\d\d\d\d)/.exec(s5)
["Jun 3, 2011", "Jun", "3", "2011"]
Do not reinvent, there exists plenty of programs that do what you want since its a fairly common problem. 不要重新发明,因为它是一个相当普遍的问题,所以有很多程序可以满足您的需求。 Try reading this http://javatechniques.com/blog/dateformat-and-simpledateformat-examples/ or just surf stackoverflow a little and you'll find plenty!
尝试阅读此http://javatechniques.com/blog/dateformat-and-simpledateformat-examples/或仅浏览stackoverflow一点,您会发现很多!
/\w+\s\d+(st)?(nd)?(rd)?(th)?,\s+\d+/
More comprehensive regex to accept forms that may not match the exact typed month but match the form of "Month Day(optional suffix), Year 更全面的正则表达式,可以接受与确切键入的月份不匹配但与“月份(可选)”,“年份”匹配的形式
Note that you could have something that looked like: 请注意,您可能会看到以下内容:
Blah 45rd, 2022222 Blah 45rd,2022222
And it would still catch it. 而且它仍然会抓住它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.