I need to make pattern which will accept String in format (_,_)
e.g: "(0,0)"
, "(1,3)"
, "(5,8)"
etc.
I wrote condition as following:
if (name.equals("(\\d,\\d)")) {
System.out.println("accepted")
}
I need to make pattern which will accept String in format (_,_)
e.g: "(0,0)"
, "(1,3)"
, "(5,8)"
etc.
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()
.
name.equals()
doesn't actually accept a regular expression. You're looking for matches()
, which will accept a regular expression.
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");
}