Results 1 to 3 of 3
Thread: Regular Expression for Date
- 01-22-2011, 11:44 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 11
- Rep Power
- 0
Regular Expression for Date
Hi. I have written a regular expression that catches the date from XML file.
String object "summary" contains something like...
Java Code:When: Fri Dec 24, 2010 10am to 11am? JST<br /> <br />Where: ?192-0005??????????354?042-691-1847 <br />Event Status: confirmed
I added both "Sept" and "Sep" for September just to make sure.
the regular expression seems OK. I wrote a separate Java code that confirms its accuracy.
But for some reason, the code does not pick up the date at all. Hopefully, "addlist" object gets the date.
It there any problem in the code?
then, the code follows.
Thanks in advance.Java Code:SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy"); String date_exp = "(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sept|Sep|Oct|Nov|Dec)\\s*[01]?[0-9],\\s*[0-9]{4}"; Pattern pd = Pattern.compile(date_exp); Matcher md = pd.matcher(summary); if (md.find()) { try { date = sdf.parse(md.group()); addlist.setDate(date); System.out.println("entered date is "+addlist.getDate()); } catch (ParseException e) { //e.printStackTrace(); } } else { System.out.println("date is NOT entered"); }
soichi
- 01-22-2011, 12:13 PM #2
Senior Member
- Join Date
- Dec 2010
- Posts
- 165
- Rep Power
- 3
you not have to use such a complicated regex. since your date is always in between "When:" and "</br>", then you can just capture whatever is between these 2 patterns. eg
(not tested, but you get the idea)
make sure you compile with Multiline mode though.Java Code:"When:(.*?)</br>"
Or you can simply use string manipulation
you basically split the string on "</br>", then go through the array, looking for "When:". When found, do a split again on "When" and get the 2nd element . Or you can use substring() etc as you see fit.Java Code:String s[] = summary.split("</br>") for (int i=0 ; i<s.length; ++){ if ( s[i].contains("When:") ){ String[] d = s[i].split("When:"); System.out.println ( "s[1] should contain your date"); } }Last edited by JavaHater; 01-22-2011 at 12:16 PM.
- 01-23-2011, 12:51 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 11
- Rep Power
- 0
Similar Threads
-
Help with regular expression
By mr.ab18 in forum New To JavaReplies: 2Last Post: 08-06-2010, 10:01 PM -
regular expression
By prof.deedee in forum JDBCReplies: 3Last Post: 02-19-2010, 11:15 AM -
regular expression
By QkrspCmptPop in forum Advanced JavaReplies: 8Last Post: 01-20-2010, 03:55 AM -
regular expression
By ras_pari in forum Advanced JavaReplies: 27Last Post: 10-07-2009, 12:25 PM -
regular expression for unicode
By tharhan in forum Advanced JavaReplies: 0Last Post: 04-01-2008, 10:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks