1

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")
}
2
  • String#equals does not use regular expressions. Commented Apr 9, 2014 at 0:18
  • As stated in the Regex Wiki FAQ, under "Flavor-specific meta-information" (third section from the bottom), the only java.lang.String functions that accept regular expressions are matches(s), replaceAll(s,s), replaceFirst(s,s), split(s), and split(s,i) Commented Apr 9, 2014 at 0:32

2 Answers 2

3

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().

Sign up to request clarification or add additional context in comments.

Comments

1

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");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.