Java Pattern打印捕获组

((\d{1,2})/(\d{1,2})/(\d{2,4})) 

有没有办法使用Pattern对象检索所有捕获组的列表。 我调试了对象,它说的是有多少组(5)。

我需要检索以下捕获组的列表。

输出示例:

 0 ((\d{1,2})/(\d{1,2})/(\d{2,4})) 1 (\d{2})/(\d{2})/(\d{4}) 2 \d{2} 3 \d{2} 4 \d{4} 

更新:

我不一定要问是否存在正则表达式,但这是最有利的。 到目前为止,我已经创建了一个基本的解析器(我没有检查大多数越界条件),它只匹配最内层的组。 我想知道是否有办法保持对已访问过的括号的引用。 我可能要实现树结构?

 import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; public class App { public final char S = '('; public final char E = ')'; public final char X = '\\'; String errorMessage = "Malformed expression: "; /** * Actual Output: * Groups: [(//), (\d{1,2}), (\d{1,2}), (\d{2,4})] * Expected Output: * Groups: [\\b((\\d{1,2})/(\\d{1,2})/(\\d{2,4}))\\b, ((\\d{1,2})/(\\d{1,2})/(\\d{2,4})), (\d{1,2}), (\d{1,2}), (\d{2,4})] */ public App() { String expression = "\\b((\\d{1,2})/(\\d{1,2})/(\\d{2,4}))\\b"; String output = ""; if (isValidExpression(expression)) { List groups = findGroups(expression); output = "Groups: " + groups; } else { output = errorMessage; } System.out.println(output); } public List findGroups(String expression) { List groups = new ArrayList(); int[] pos; int start; int end; String sub; boolean done = false; while (expression.length() > 0 && !done) { pos = scanString(expression); start = pos[0]; end = pos[1]; if (start == -1 || end == -1) { done = true; continue; } sub = expression.substring(start, end); expression = splice(expression, start, end); groups.add(0, sub); } return groups; } public int[] scanString(String str) { int[] range = new int[] { -1, -1 }; int min = 0; int max = str.length() - 1; int start = min; int end = max; char curr; while (start  -1 && end <= max) { curr = str.charAt(end); if (curr == E) { range[1] = end + 1; break; } end++; } return range; } public String splice(String str, int start, int end) { if (str == null || str.length() < 1) return ""; if (start  str.length()) { System.err.println("Positions out of bounds."); return str; } if (start >= end) { System.err.println("Start must not exceed end."); return str; } String first = str.substring(0, start); String last = str.substring(end, str.length()); return first + last; } public boolean isValidExpression(String expression) { try { Pattern.compile(expression); } catch (PatternSyntaxException e) { errorMessage += e.getMessage(); return false; } return true; } public static void main(String[] args) { new App(); } } 

这是我的解决方案……我只是提供了正则表达式的正则表达式,因为@SotiriosDelimanolis已经注释掉了。

 public static void printGroups() { String sp = "((\\(\\\\d\\{1,2\\}\\))\\/(\\(\\\\d\\{1,2\\}\\))\\/(\\(\\\\d\\{2,4\\}\\)))"; Pattern p = Pattern.compile(sp); Matcher m = p.matcher("(\\d{1,2})/(\\d{1,2})/(\\d{2,4})"); if (m.matches()) for (int i = 0; i <= m.groupCount(); i++) System.out.println(m.group(i)); } 

注意你不能删除if -statement,因为为了使用group方法你应该先调用matches方法(我不知道!)。 请参阅此链接作为参考。

希望这是你要求的......