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

Matcher#replaceAll gives wrong result for .*

    XMLWordPrintable

Details

    Description

      A DESCRIPTION OF THE PROBLEM :
      java.util.regex.Matcher#replaceAll(java.lang.String) gives wrong result for the regular expression .*

      Code:
      result = Pattern.compile(".*").matcher("test").replaceAll("X");

      Expected Result: "X"
      Actual Result: "XX"
      The first occurrence of the replacement string stems from the expected match of the whole input "test".
      The second replacement string stems from an additional match of the empty string at the end of the input. This match should not occur.

      The pattern ".*$" does not prevent the additional match.


      ---------- BEGIN SOURCE ----------
      result = Pattern.compile(".*").matcher("test").replaceAll("X");
      assertEquals("X", result);
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use the pattern "^.*" instead of " .*"

      FREQUENCY : always


      Attachments

        Activity

          People

            smarks Stuart Marks
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: