Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-7171390

replaceAll matches incorrectly based on input and order of regex

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 7
    • core-libs

      FULL PRODUCT VERSION :
      JDK 1.7 u4

      ADDITIONAL OS VERSION INFORMATION :
      Windows 7 x64

      A DESCRIPTION OF THE PROBLEM :
      Take the

      1 - String r1="blah - * ^% $^& [gf]".replaceAll("[a-zA-Z0-9]", "-");
      2 - String r2="blah - * ^% $^& [gf]".replaceAll("[0-9a-zA-z]", "-");
      3 - String r3="this is a test [s] ^^ some chars blah-*^%$^&[gf]".replaceAll("[0-9a-zA-Z]","-");

      produces
      r1="---- - * ^% $^& [--]"
      r2="---- - * -% $-& ----"
      r3="---- -- - ---- [-] ^^ ---- ----- -----*^%$^&[--]"

      You would expect that r1 equals r2, as they have equivalent regexes however the matcher when given the regex in number 2 incorrectly matches control characters including the square brackets in the regex and carat characters not listed in the regex.

      In addition the matching seems to also be dependent on the input string, for example given the behaviour of line 2, you would expect r3 to equal:
      r3="---- -- - ---- --- -- ---- ----- -----*-%$-&----"

      but it in fact matches correctly, despite the only difference being the input string.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      run the following code to observe the issue:

      package test;
      public class Test {
          public static void main(String[] args) {
              String r0="this is a test [s] ^^ some chars blah-*^%$^&[gf]".replaceAll("[0-9a-zA-Z]","-");
              String r1="blah - * ^% $^& [gf]".replaceAll("[0-9a-zA-z]", "-");
              String r2="blah - * ^% $^& [gf]".replaceAll("[a-zA-Z0-9]", "-");
              System.out.println(result);
              System.out.println(r1);
              System.out.println(r2);
              System.out.println(s);
          }
      }


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      See description
      ACTUAL -
      See description

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      see above
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      rejig the regex to work

            sherman Xueming Shen
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: