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

Pattern.quote could return metachar-free strings as is

XMLWordPrintable

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

      Run:

      ---%<---
      import java.util.regex.Pattern;
      public class PatternQuote {
          public static void main(String[] x) {
              System.out.println(Pattern.quote("foo*bar"));
              System.out.println(Pattern.quote("foo\\Ebar"));
              System.out.println(Pattern.quote("foobar"));
          }
      }
      ---%<---

      I get:

      ---%<---
      java version "1.7.0-ea"
      Java(TM) SE Runtime Environment (build 1.7.0-ea-b04)
      Java HotSpot(TM) Client VM (build 1.7.0-ea-b04, mixed mode, sharing)

      \Qfoo*bar\E
      \Qfoo\E\\E\Qbar\E
      \Qfoobar\E
      ---%<---

      All are correct, but the last is needlessly complex; just "foobar" would suffice.

      Of course you would be unlikely to write the third call to P.q with a string literal like this. But it is common for some code to construct a regex from various pieces, some literals that must be quoted, some patterns inserted by the algorithm. With the current impl of P.q you can wind up with a path-matching expression such as

        ^\Qsome\E\Q/\E\Qdir\E\Q\/\E.*\Q/\E\Qsome.file\E$

      which is far harder to read, when debugging, than the equivalent

        ^some/dir/.*/\Qsome.file\E$

            sherman Xueming Shen
            jglick Jesse Glick (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: