Regular expression to extract Date value specified in word from the string in Java
I have the Date column which contains the following sa开发者_如何转开发mple values
- Posted on June 25, 2010 at 1:01 PM
- March 14, 2011
- Friday, April 15, 2011 12:15 am
- Thursday, March 31st, 2011, 1:11pm
- Updated: 9:34 am, Fri Jun 3, 2011.
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.
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.
Sample output from 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!
/\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
And it would still catch it.
精彩评论