[英]My regex pattern does not work
I need to make pattern which will accept String in format (_,_)
eg: "(0,0)"
, "(1,3)"
, "(5,8)"
etc. 我需要制作将接受格式为(_,_)
String的模式,例如: "(0,0)"
, "(1,3)"
, "(5,8)"
等。
I wrote condition as following: 我写条件如下:
if (name.equals("(\\d,\\d)")) {
System.out.println("accepted")
}
You need to escape the parenthesis with \\
. 您需要使用\\
转义括号。 They have special meaning in regex (for grouping). 它们在正则表达式(用于分组)中具有特殊含义。
You also need to call the correct method, one which matches with a regex, which isn't equals()
, but matches()
. 您还需要调用正确的方法,该方法与正则表达式匹配,该方法不是equals()
,而是matches()
。
name.equals()
doesn't actually accept a regular expression. name.equals()
实际上并不接受正则表达式。 You're looking for matches()
, which will accept a regular expression. 您正在寻找matches()
, 它将接受一个正则表达式。
You'll also have to escape the parentheses, as they have special meaning in a regular expression. 您还必须转义括号,因为它们在正则表达式中具有特殊含义。
if(name.matches("\\(\\d,\\d\\)") {
System.out.println("accepted");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.