Would be great to have convenient way of storing patterns with single backslash. Some workarounds: store it in the file and use NIO to read. Cons: Java EE does not allow IO access. Store somehow in JNDI. Maybe new to java 5 Pattern.LITERAL flag can help? I want to work with normal pattern string, like \d, not \\d.
解决方案
the trouble is that \ is a special char in java when creating a String, regardless of regexp or not.
eg String s = "\t";
you cannot use this for arbitrary chars though, String s = "\a"; will give you a compile-time error. the valid chars are b, t, n, f, r, ", ' and \
therefore to get a literal \ in a string in java you need to escape it like so : \\. because of that, your only option is to NOT have these strings in java files, hence in an external file that is loaded by your java file. Pattern.LITERAL won't help at all because you still need a valid java string, which \d isn't.