Wednesday, 8 February 2017

Regex : Group






public class GroupInsideGroup {

    public static void main(String[] args) {

        String text    =
                "John writes about this, and John Doe writes about that," +
                        " and John Wayne writes about everything."                ;
        String patternString1 = "((John) (.+?)) ";
        Pattern pattern = Pattern.compile(patternString1);        Matcher matcher = pattern.matcher(text);
        while(matcher.find()) {
            System.out.println("found: <"  + matcher.group(1) +
                    "> <"       + matcher.group(2) +
                    "> <"       + matcher.group(3) + ">");        }
    }

}

No comments:

Post a Comment