Auf Thema antworten

Ich stimme njans zu.

Habe deine Aussagen mal in einen jUnit-Test gegossen

[code=Java]public class TestRegex {


    @Test

    public void test() {

        boolean match = testPattern("Fred", "M%");

        assertEquals(false, match);

        match = testPattern("Fred", "M_");

        assertEquals(false, match);

        match = testPattern("Mike", "M%");

        assertEquals(true, match);

        match = testPattern("Martin", "M%");

        assertEquals(true, match);

        match = testPattern("Billy", "M%");

        assertEquals(false, match);

        match = testPattern("Momo", "M%");

        assertEquals(true, match);

    }


    public boolean testPattern(String txt, String pattern) {

        String regex = pattern.replace("%", ".*").replace("_", ".");

        boolean matches = txt.matches(regex);

        System.out.println(txt + " matches(" + pattern + "[" + regex + "])\t" + matches);

        return matches;

    }

}[/code]



Oben