这很奇怪,因为它是非常简单的dd/mm
格式正则表达式。结果应该是:"Group 1: 14; Group 2: 12"
但它是"Group 1: 14; Group 2: 1"
。
第二组只捕获了第一个字符,但省略了第二个字符(示例中为“2”)。
String sDay = "(?:0?[1-9]|[12][0-9]|3[01])";
String sMonth = "(?:0?[1-9]|1[0-2])";
String sDot = "[\\.]";
String sSlash = "[/]";
String sMinus = "[\\-]";
String sSeparators = (sDot + "|" + sSlash + "|" + sMinus);
Pattern reDayMonth =
Pattern.compile("(" + sDay + ")" + "(?:" + sSeparators + ")" + "(" + sMonth+ ")");
String s = "14/12";
Matcher reMatcher = reDayMonth.matcher(s);
boolean found = reMatcher.find();
System.out.println("Group 1: " + reMatcher.group(1) + "; Group 2: " + reMatcher.group(2));
我不明白为什么。请你帮助我好吗?