Results 1 to 3 of 3
  1. #1
    soichi is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default 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.

    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");
    }
    Thanks in advance.

    soichi

  2. #2
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    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)
    Java Code:
    "When:(.*?)</br>"
    make sure you compile with Multiline mode though.

    Or you can simply use string manipulation
    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");
       }
    }
    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.
    Last edited by JavaHater; 01-22-2011 at 01:16 PM.

  3. #3
    soichi is offline Member
    Join Date
    Jan 2011
    Posts
    14
    Rep Power
    0

    Default

    Thanks for your help!
    It is much easier than I thought.

    soichi

Similar Threads

  1. Help with regular expression
    By mr.ab18 in forum New To Java
    Replies: 2
    Last Post: 08-06-2010, 11:01 PM
  2. regular expression
    By prof.deedee in forum JDBC
    Replies: 3
    Last Post: 02-19-2010, 12:15 PM
  3. regular expression
    By QkrspCmptPop in forum Advanced Java
    Replies: 8
    Last Post: 01-20-2010, 04:55 AM
  4. regular expression
    By ras_pari in forum Advanced Java
    Replies: 27
    Last Post: 10-07-2009, 01:25 PM
  5. regular expression for unicode
    By tharhan in forum Advanced Java
    Replies: 0
    Last Post: 04-01-2008, 11:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •