您忘记了您的模式中的单词Robot
。此外,括号在正则表达式特殊字符,应该+
后不(
放在\d
后:
Pattern.compile("^createRobot\\(\\d+,\\d+\\)$")
请注意,如果您想验证输入应该只包括本"createRobot"
-string,你心如做:
boolean success = s.matches("createRobot\\(\\d+,\\d+\\)");
其中s
是要验证String
。但是,如果你想获取相匹配的数字,你做需要使用模式/匹配器:
Pattern p = Pattern.compile("createRobot\\((\\d+),(\\d+)\\)");
Matcher m = p.matcher("createRobot(12,345)");
if(m.matches()) {
System.out.printf("x=%s, y=%s", m.group(1), m.group(2));
}
正如你所看到的,调用Matcher.matches()
(或Matcher.find()
)后,就可以检索ñ th match-group through group(n)
。
如果它应该是createRobot,你为什么在表达中只有“创造”? – stivlo
我忘了发布它,但在代码中我有“createRobot”,对不起 – JuanS