Code example for Matcher

Methods: replaceFirst

        // Begins with matching text, ends with non-matching text 
        p = Pattern.compile("a*b");
        m = p.matcher("aabfooaabfooabfoobfoo");
        r = m.replaceFirst("-");
        assertEquals("-fooaabfooabfoobfoo", r);
    public void testSplit() throws Exception { 
        Pattern p = Pattern.compile(":");
        String[] strings;
        strings = p.split("boo:and:foo");
        assertEquals(3, strings.length);
        assertEquals("boo", strings[0]);
        assertEquals("and", strings[1]);
        assertEquals("foo", strings[2]);
        strings = p.split("boo:and:foo", 2);
Experience pair programming with AI  Get Codota for Java