I need validate a String using a Regular Expression, the String must be like “createRobot(x,y)”, where x and y are digits.
I have Something like
String ins;
Pattern ptncreate= Pattern.compile("^createRobot(+\\d,\\d)");
Matcher m = ptncreate.matcher(ins);
System.out.println(m.find());
but doesn’t work
Can you help me ?.
Thanks.
You forgot the word
Robotin your pattern. Also, parenthesis are special characters in regex, and the+should be placed after the\d, not after a(:Note that if you want to validate input that should consist solely of this
"createRobot"-string, you mind as well do:where
sis theStringyou want to validate. But if you want to retrieve the numbers that were matched, you do need to use a Pattern/Matcher:As you can see, after calling
Matcher.matches()(orMatcher.find()), you can retrieve the nth match-group throughgroup(n).