// Creates a two-character string: "\\"
// When used as a regex pattern, will match a single character: "\"
String regexPattern = "\\\\";
在您的例子中,您可能没有转义所用语言中的反斜杠字符:
// will most likely result in an "Illegal escape character" error
String wrongPattern = "\.";
// will result in the string "\."
String correctPattern = "\\.";
First, we can clean up this expression a bit by using a 字符类速记法字符类速记法 for the digits (note that this is also susceptible to the escaping issue mentioned above):