Java Pattern print capturing groups

扶醉桌前 提交于 2019-11-28 10:35:59

Here is my solution ... I simply provided a regex of the regex as @SotiriosDelimanolis commented out.

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));
    }

Pay attention that you cannot remove the if-statement because in order to use the group method you should call the matches method first (I didn't know it!). See this link as a reference about it.

Hope this is what you were asking for ...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!