Groovy
  1. Groovy
  2. GROOVY-3287

Implement workaround for JDK 1.4 String.replaceAll bug.

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.6-rc-2, 1.5.8
    • Component/s: None
    • Labels:
      None

      Description

      There is a bug in JDK 1.4's String.replaceAll in which quoted (\Q..\E) patterns that contain a backslash are not matched correctly.

      This should work, but does not in JDK 1.4 (JDK 1.5 is OK):

      assert "axa".equals("a\\a".replaceAll("\\Q\\\\E", "x"))
      

      The workaround is to have RegexUtils.quote not put backslashes inside the quote, but instead escape out of the quote and use a backslash to escape the backslash.

      So this does work in JDK 1.4 (and JDK 1.5 too of course):

      assert "axa".equals("a\\a".replaceAll("\\Q\\E\\\\\\Q\\E", "x"))
      

        Issue Links

          Activity

          James P. White created issue -
          James P. White made changes -
          Field Original Value New Value
          Assignee Jim White [ jimwhite ]
          James P. White made changes -
          Link This issue is depended upon by GROOVY-3285 [ GROOVY-3285 ]
          Hide
          James P. White added a comment -

          The test case is:

          assert 'a\\a'.replace('\\', 'x') == 'axa'
          

          It is in groovy.StringTest.testReplace.

          Show
          James P. White added a comment - The test case is: assert 'a\\a'.replace('\\', 'x') == 'axa' It is in groovy.StringTest.testReplace .
          James P. White made changes -
          Resolution Fixed [ 1 ]
          Fix Version/s 1.6 [ 13832 ]
          Status Open [ 1 ] Resolved [ 5 ]
          Fix Version/s 1.5.8 [ 14630 ]
          James P. White made changes -
          Description There is a bug in {{String.replaceAll}} in which quoted ({{\Q..\E}}) patterns that contain a backslash are not matched correctly.

          The workaround is to have {{RegexUtils.quote}} not put backslashes inside the quote, but instead escape out of the quote and use a backslash to escape the backslash.
          There is a bug in JDK 1.4's {{String.replaceAll}} in which quoted ({{\Q..\E}}) patterns that contain a backslash are not matched correctly.

          This should work, but does not in JDK 1.4 (JDK 1.5 is OK):

          {{code}}
          assert "axa".equals("a\\a".replaceAll("\\Q\\\\E", "x"))
          {{code}}

          The workaround is to have {{RegexUtils.quote}} not put backslashes inside the quote, but instead escape out of the quote and use a backslash to escape the backslash.

          So this does work in JDK 1.4 (and JDK 1.5 too of course):

          {{code}}
          assert "axa".equals("a\\a".replaceAll("\\Q\\E\\\\\\Q\\E", "x"))
          {{code}}

          James P. White made changes -
          Description There is a bug in JDK 1.4's {{String.replaceAll}} in which quoted ({{\Q..\E}}) patterns that contain a backslash are not matched correctly.

          This should work, but does not in JDK 1.4 (JDK 1.5 is OK):

          {{code}}
          assert "axa".equals("a\\a".replaceAll("\\Q\\\\E", "x"))
          {{code}}

          The workaround is to have {{RegexUtils.quote}} not put backslashes inside the quote, but instead escape out of the quote and use a backslash to escape the backslash.

          So this does work in JDK 1.4 (and JDK 1.5 too of course):

          {{code}}
          assert "axa".equals("a\\a".replaceAll("\\Q\\E\\\\\\Q\\E", "x"))
          {{code}}

          There is a bug in JDK 1.4's {{String.replaceAll}} in which quoted ({{\Q..\E}}) patterns that contain a backslash are not matched correctly.

          This should work, but does not in JDK 1.4 (JDK 1.5 is OK):

          {code}
          assert "axa".equals("a\\a".replaceAll("\\Q\\\\E", "x"))
          {code}

          The workaround is to have {{RegexUtils.quote}} not put backslashes inside the quote, but instead escape out of the quote and use a backslash to escape the backslash.

          So this does work in JDK 1.4 (and JDK 1.5 too of course):

          {code}
          assert "axa".equals("a\\a".replaceAll("\\Q\\E\\\\\\Q\\E", "x"))
          {code}

          Paul King made changes -
          Status Resolved [ 5 ] Closed [ 6 ]
          Mark Thomas made changes -
          Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
          Mark Thomas made changes -
          Workflow jira [ 12732284 ] Default workflow, editable Closed status [ 12744106 ]
          Mark Thomas made changes -
          Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
          Mark Thomas made changes -
          Workflow jira [ 12970128 ] Default workflow, editable Closed status [ 12977906 ]
          Transition Time In Source Status Execution Times Last Executer Last Execution Date
          Open Open Resolved Resolved
          1h 14m 1 James P. White 15/Jan/09 19:40
          Resolved Resolved Closed Closed
          7d 1h 53m 1 Paul King 22/Jan/09 21:34

            People

            • Assignee:
              James P. White
              Reporter:
              James P. White
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development