10 Regular Expression Matching

public class No10 {

    public static void main(String[] args) {
        System.out.println(isMatch("aa", "a"));
        System.out.println(isMatch("aa", "aa"));
        System.out.println(isMatch("aaa", "aa"));
        System.out.println(isMatch("aa", "a*"));
        System.out.println(isMatch("aa", ".*"));
        System.out.println(isMatch("ab", ".*"));
        System.out.println(isMatch("aab", "c*a*b"));
    }

    public static boolean isMatch(String s, String p) {

        if (p.length() == 0)
            return s.length() == 0;
        else if (s.length() == 0) {
            if (p.length() > 1 && p.charAt(1) == '*')
                return isMatch(s, p.substring(2));
            else
                return false;
        } else if (p.length() > 1 && p.charAt(1) == '*') {
            if (isMatch(s, p.substring(2)))
                return true;
            else if (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.')
                return isMatch(s.substring(1), p);
            else
                return false;
        } else
            return (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.')
                    && isMatch(s.substring(1), p.substring(1));

    }

}

你可能感兴趣的:(10 Regular Expression Matching)