This discussion is archived
5 Replies Latest reply: Jan 8, 2013 7:00 AM by doremifasollatido RSS

Java and Regular Expression

976910 Newbie
Currently Being Moderated
Hi all,
i confuse why the follwoing code replace the date with first one in string
String inputString= "hell to yemen on 8 JAN 2012 or on 11 FEB 2004";

Matcher m2 = Pattern.compile("([0-9]{1,2}) (JAN|FEB|MAR|APR|JUN|JUL|AUG|SEP|OCT|NOV|DEC|JANUARY|FEBRUARY|MARCH|APRIL|JUNE|JULY|AUGUST|SEPTEMBER|OCTOBER|NOVEMBER|DECEMBER) ([0-9]{4})", Pattern.CASE_INSENSITIVE).matcher(inputString);
  String  resultString=null;
 String temp_str=null;
 String first=null;
  String second=null;
   String month=null;
while (m2.find()) {
    if (m2.groupCount()==3) {

    first = m2.group(1);
         second = m2.group(2);
         month = m2.group(3);
       System.out.println("three parts"+month);
        temp_str=m2.replaceAll(first+"-"+second+"-"+month);
          //temp_str=m2.replaceFirst(second)

       System.out.println(temp_str);
    }
}
the expected reults is
hell to yemen on 8-JAN-2012 or on 11-FEB-2004
not why the like this that now appear
hell to yemen on 8-JAN-2012 or on 8-JAN-2012
Edited by: 973907 on 07-Jan-2013 02:34
  • 1. Re: Java and Regular Expression
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    You invoke <tt>replaceAll</tt> on the matcher.

    <tt>replaceAll</tt> changes all occurences of the pattern (both dates) with the same String given as parameter. Thats why after the first iteration both dates are the same.

    bye
    TPD
  • 2. Re: Java and Regular Expression
    aksarben Journeyer
    Currently Being Moderated
    As an aside: Did you mean to say "*Hello* to Yemen"?
  • 3. Re: Java and Regular Expression
    976910 Newbie
    Currently Being Moderated
    Many thanks for you.
    you are right but now the when i print the string appear only half of it why?
    String inputString="hell to yemen on 8 JAN 2012 or on 11 FEB 2004 hi 12 MAR 2009 or on 11 MAY 2005 or 08 2007 and can be 2011 DEC or 2 ";
    Pattern p = Pattern.compile("([0-9]{1,2}) (JAN|FEB|MAR|APR|JUN|JUL|AUG|SEP|OCT|NOV|DEC|JANUARY|FEBRUARY|MARCH|APRIL|JUNE|JULY|AUGUST|SEPTEMBER|OCTOBER|NOVEMBER|DECEMBER) ([0-9]{4})");
        StringBuffer sb = new StringBuffer();
    
        String candidateString = inputString;
        String replacement = null;
    String first=null;
      String second=null;
       String third=null;
        Matcher matcher = p.matcher(candidateString);
        while (matcher.find())
        {
    
             first = matcher.group(1);
             second = matcher.group(2);
             third = matcher.group(3);
       replacement = first+"-"+second+"-"+third;
    if (matcher.groupCount()==3) {
        matcher.appendReplacement(sb, replacement);
    }
        }
        String msg = sb.toString();
        System.out.println(msg); 
    the results is :
    hell to yemen on 8-JAN-2012 or on 11 FEB 2004 hi 12-MAR-2009
    but the expected results is :
    hell to yemen on 8-JAN-2012 or on 11-FEB-2004 hi 12-MAR-2009 or on 11-MAY-2005 or 08 2007 and can be 2011 DEC or 2 JULY or JUN 2  uuu"
    {code}
    
    Edited by: 973907 on Jan 8, 2013 2:39 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
  • 4. Re: Java and Regular Expression
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    API wrote:
    http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#appendReplacement(java.lang.StringBuffer,%20java.lang.String)
    This method performs the following actions:

    It reads characters from the input sequence, starting at the append position, and appends them to the given string buffer. It stops after reading the last character preceding the previous match, that is, the character at index start() - 1.

    It appends the given replacement string to the string buffer.

    It sets the append position of this matcher to the index of the last character matched, plus one, that is, to end().
    After your first iteration the matcher does not contain the rest of the input string anymore.

    [edit] You should consider <tt>String.split()</tt> or the depricated <tt>StringTokenizer</tt> since the latter can return the matching part too. (But you use of <tt>StringBuffer</tt> shows that you don't care for depricated API anyway...)

    bye
    TPD

    Edited by: TPD Opitz-Consulting com on 08.01.2013 12:07
  • 5. Re: Java and Regular Expression
    doremifasollatido Journeyer
    Currently Being Moderated
    [edit] You should consider <tt>String.split()</tt> or the depricated <tt>StringTokenizer</tt> since the latter can return the matching part too. (But you use of <tt>StringBuffer</tt> shows that you don't care for depricated API anyway...)
    Neither StringTokenizer nor StringBuffer are deprecated. StringTokenizer is discouraged in new code, but it is not marked as deprecated. StringBuilder should be used for single-thread use (the most common case) because the synchronization of StringBuffer is unnecessary in such cases, but StringBuffer is not marked as deprecated, either.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points