4 Replies Latest reply: May 29, 2014 1:58 PM by jschellSomeoneStoleMyAlias RSS

    Different results using Matcher.replaceAll on a literal depending on the Pattern compiled

    3dd740db-eede-43cc-bb6d-3406fc3302fb

      I would have expected that the results for all the following scenarios would have been the same:

       

      public class PatternMatcher {

          public static void main(String[] args) {

              Pattern p;

              Matcher m;

       

              // duplicates

              p = Pattern.compile("(.*)");

              m = p.matcher("abc");

              if (m.matches()) System.out.println(p + " : " + m.replaceAll("xyz"));

       

              // single

              p = Pattern.compile("(.+)");

              m = p.matcher("abc");

              if (m.matches()) System.out.println(p + " : " + m.replaceAll("xyz"));

       

              // wtf

              p = Pattern.compile("(.*?)");

              m = p.matcher("abc");

              if (m.matches()) System.out.println(p + " : " + m.replaceAll("xyz"));

       

              // duplicates

              p = Pattern.compile("(.*+)");

              m = p.matcher("abc");

              if (m.matches()) System.out.println(p + " : " + m.replaceAll("xyz"));

       

              // single

              p = Pattern.compile("^(.*)");

              m = p.matcher("abc");

              if (m.matches()) System.out.println(p + " : " + m.replaceAll("xyz"));

       

              // duplicates

              p = Pattern.compile("(.*)$");

              m = p.matcher("abc");

              if (m.matches()) System.out.println(p + " : " + m.replaceAll("xyz"));

       

              // single

              p = Pattern.compile("^(.*)$");

              m = p.matcher("abc");

              if (m.matches()) System.out.println(p + " : " + m.replaceAll("xyz"));

       

              // single

              p = Pattern.compile("(.(.*).)");

              m = p.matcher("abc");

              if (m.matches()) System.out.println(p + " : " + m.replaceAll("xyz"));

          }

      }

       

      But the results vary depending on the pattern compiled:

      (.*) : xyzxyz

      (.+) : xyz

      (.*?) : xyzaxyzbxyzcxyz

      (.*+) : xyzxyz

      ^(.*) : xyz

      (.*)$ : xyzxyz

      ^(.*)$ : xyz

      (.(.*).) : xyz

       

       

      Since all of the patterns have an all-encompassing capture group, but the replacement string does not have any group references, I was expecting that in every case the replacement string would simply be returned unchanged (so just "xyz").

       

      Have I uncovered a bug in the core library?  or am I misunderstanding how this should be working?