Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中匹配反斜杠?_Java_Regex - Fatal编程技术网

如何在Java中匹配反斜杠?

如何在Java中匹配反斜杠?,java,regex,Java,Regex,我想知道一个字符串是否包含两个斜杠。正斜杠很容易检查 String test = "12/13/2013"; boolean slash = test.matches("\\d+\\/\\d+\\/\\d+"); 但是如何检查反斜杠 String test = "12\13\2013"; boolean slash = test.matches("\\d+\\\\\\d+\\\\\\d+"); 上面不承认吗?我还尝试了(“\\d+\\\d+\\\\d+”您正确地转义了正则表达式,但没有正

我想知道一个字符串是否包含两个斜杠。正斜杠很容易检查

String test = "12/13/2013";
boolean slash = test.matches("\\d+\\/\\d+\\/\\d+");

但是如何检查反斜杠

String test = "12\13\2013";
boolean slash = test.matches("\\d+\\\\\\d+\\\\\\d+"); 

上面不承认吗?我还尝试了
(“\\d+\\\d+\\\\d+”

您正确地转义了正则表达式,但没有正确地转义测试字符串。试一试

String test = "12\\13\\2013";

有趣的是,您的代码
String test=“12\13\2013”
不会编译,因为您无意中通过字符的八进制转义指定了字符,这些字符由反斜杠后跟八进制数指定,从
\000
\377
。也就是说,
\13
\201
是八进制转义。

这个
\
是反斜杠,这个
/
是正斜杠。
“12\13\2013”
->
“12\\13\\2013”
。您需要以所有文字转义\谢谢,我现在就知道了