5 Replies Latest reply: Jan 8, 2013 9:00 AM by doremifasollatido RSS

    Java and Regular Expression

      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";
        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);
      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
          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.

          • 2. Re: Java and Regular Expression
            As an aside: Did you mean to say "*Hello* to Yemen"?
            • 3. Re: Java and Regular Expression
              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 ";
                  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();
              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"
              Edited by: 973907 on Jan 8, 2013 2:39 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
              • 4. Re: Java and Regular Expression
                API wrote:
                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...)


                Edited by: TPD Opitz-Consulting com on 08.01.2013 12:07
                • 5. Re: Java and Regular Expression
                  [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.