简体   繁体   English

正则表达式,用于从Java字符串中提取单词中指定的日期值

[英]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 我有日期列,其中包含以下示例值

  • Posted on June 25, 2010 at 1:01 PM 发表于20106月25日,下午1:01
  • March 14, 2011 2011年3月14日
  • Friday, April 15, 2011 12:15 am 2011年4月15日,星期五,上午12:15
  • Thursday, March 31st, 2011 , 1:11pm 2011年3月31日 ,星期四,1:11pm
  • Updated: 9:34 am, Fri Jun 3, 2011 . 更新时间: 2011年6月3日,上午9:34。

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. 但是没有执行stndrdth规则。

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.

相关问题 从Java中的字符串$ {VALUE(word_to_be_extracted)}中提取单词的正则表达式 - Regular expression to extract the word from the string ${VALUE(word_to_be_extracted)} in java 正则表达式Java从字符串中提取余额 - Regular expression java to extract the balance from a string 此字符串的正则表达式以提取值 - regular expression for this string to extract the value 在Java中使用正则表达式从字符串中提取日期,格式为01OCT12 14:26 - Using regular expression in Java to extract date from string in the format 01OCT12 14:26 如何从指定的字符串中提取值:java - How to extract value from a specified string : java 在Java中使用正则表达式从字符串中提取信息 - Using Regular Expression in Java to extract information from a String 使用Java中的正则表达式从字符串中提取双精度或整数? - extract a double or integer from a string using regular expression in java? 正则表达式帮助-从字符串中提取值,而忽略先前的内容 - regular expression Help on - extract a value from a string ignoring the previous contents java:使用正则表达式提取字符串中的数字 - java: use regular expression to extract a number in a string 如何使用Java正则表达式提取特定单词前后的字符串 - How to extract a string after and before a specific word using Java regular expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM