-
Bug
-
Resolution: Unresolved
-
P4
-
None
-
7
-
x86
-
linux
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$
---%<---
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$