您忘記了您的模式中的單詞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