Code example for Matcher

Methods: find, group, groupCount

0
    public void testGroups() throws Exception { 
        Pattern p = Pattern.compile("(b)([c|d])(z*)");
        Matcher m = p.matcher("abcdefg");
 
        /* Must call find() first, otherwise group*() are undefined. */ 
        assertTrue(m.find());
 
        assertEquals(3, m.groupCount());
 
        assertEquals("bc", m.group(0));
        assertEquals("b", m.group(1));
        assertEquals("c", m.group(2));
        assertEquals("", m.group(3));
    } 
 
    public void testFind() throws Exception { 
        Pattern p = Pattern.compile(".");
        Matcher m = p.matcher("abc");
 
        assertTrue(m.find());
        assertEquals("a", m.group(0));