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

OutOfBoundsException when using String.replaceAll with regular expression

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • 7-pool
    • 7u67
    • core-libs
    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.7.0_67"
      Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
      Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
                  String s1 = "1234 abc 1234 abc";
                  //Working well
                  String s2 = s1.replaceAll("1234", "2345");
                  System.out.println("s2: " + s2);

                  //Working well
                  String s3 = s1.replace("abc", "xzy\\");
                  System.out.println("s3: " + s3);

                  //Failed
                  String s4 = s1.replaceAll("abc", "xzy\\");
                  System.out.println("s4: " + s4);


      Output:
      s2: 2345 abc 2345 abc
      s3: 1234 xzy\ 1234 xzy\
      java.lang.StringIndexOutOfBoundsException: String index out of range: 4
      at java.lang.String.charAt(String.java:658)
      at java.util.regex.Matcher.appendReplacement(Matcher.java:762)
      at java.util.regex.Matcher.replaceAll(Matcher.java:906)
      at java.lang.String.replaceAll(String.java:2162)
      at com.donriver.finance.kiftool.util.Test1.main(Test1.java:27)






      REPRODUCIBILITY :
      This bug can be reproduced always.

            alitvinov Anton Litvinov (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: