我想弄清楚一个字符串包含两个斜杠.正斜杠很容易检查
String test = "12/13/2013";
boolean slash = test.matches("\\d+\\/\\d+\\/\\d+");
Run Code Online (Sandbox Code Playgroud)
但是如何检查反斜杠?
String test = "12\13\2013";
boolean slash = test.matches("\\d+\\\\\\d+\\\\\\d+");
Run Code Online (Sandbox Code Playgroud)
以上不承认吗?我也试过了("\\d+\\\\d+\\\\d+")
你正确地逃脱了你的正则表达式,但你没有正确地逃避你的测试字符串.尝试
String test = "12\\13\\2013";
Run Code Online (Sandbox Code Playgroud)
有趣的是,你的代码String test = "12\13\2013";不会编译,因为你无意中其指定的字符八进制转义,这是由一个反斜线后跟一个八进制数,从指定的\000通过\377.即\13,\201八角形逃脱.