The sword refers to the second edition of the offer interview question 19: regular expression matching (java)

Topic description: Please implement a function to match regular expressions
including '.' and ' '. The character '.' in the pattern means any character, and the ' ' means that the character preceding it can appear any number of times (including 0). In this question, match means that all characters of the string match the entire pattern. For example, the string "aaa" matches the patterns "aa" and "ab*ac*a", but neither "aa.a" nor "ab*a"

Analysis:
The core of this question is to analyze ' '. For '.', it matches any character and can be regarded as a normal character. For the analysis of ' ', we have to discuss the situation by situation. When all the situations are clarified, we can write the code.
In each round of matching, when the second character of the pattern is '*':

1. The first character does not match ('.' and any character are regarded as matching), then '*' can only represent 0 matches, such as 'ba' and 'a*ba', the string remains unchanged, and the pattern is backward Move two characters, and then match the remaining string to match pattern
2 and the first character, then '*' may represent 0, 1, or multiple matches, such as 'aaa' and 'a*aaa', 'aba' with 'a*ba', 'aaaba' and 'a*ba'. When matched 0 times, the string is unchanged, the pattern is moved back two characters, and then the remaining strings and patterns are matched; when matched once, the string is moved back by one character, and the pattern is moved back by 2 characters; matched multiple times When , the string moves one character backward, and the mode remains unchanged;

And when the second character of the pattern is not '*', the situation is much simpler:

1. If the first character of the string matches the first character in the pattern, then move back one character on both the string and the pattern, and then match the remaining string and pattern.
2. If the first character of the string does not match the first character in the pattern, return false directly.

code show as below:

/**
 * 正则表达式匹配
 */
public class RegularMatch {

    /**
     * @param str 字符串
     * @param pattern 模式
     * @return
     */
    public boolean match(char[] str,char[] pattern){
        //参数校验
        if(str == null || pattern == null || str.length == 0 || pattern.length == 0){
            return false;
        }

        return matchCore(str, 0, pattern, 0);
    }

    public boolean matchCore(char[] str, int strIndex, char[] pattern, int pIndex){
        //字符串和模式都已操作完,返回true
        if(strIndex >= str.length && pIndex >= pattern.length)
            return true;
        //字符串没有操作完,模式操作完,返回false
        if(strIndex < str.length && pIndex >= pattern.length)
            return false;
        //字符串操作完,模式没有操作完
        if(strIndex >= str.length && pIndex < pattern.length){
            if(pIndex + 1 < pattern.length && pattern[pIndex + 1] == '*')
                return matchCore(str, strIndex, pattern, pIndex+2);
            else
                return false;
        }

        /**
         * 字符串没有操作完,模式没有操作完
         */
        //如果模式的下一个字符为*
        if(pIndex + 1 < pattern.length && pattern[pIndex+1] == '*'){
            //字符串和模式的当前字符能够匹配
            if(str[strIndex] == pattern[pIndex]){
                return matchCore(str, strIndex, pattern, pIndex+2)
                        ||matchCore(str, strIndex+1, pattern, pIndex+2)
                        ||matchCore(str, strIndex+1, pattern, pIndex);
            }else{
                return matchCore(str, strIndex, pattern, pIndex+2);
            }
        }
        else{
            if(str[strIndex] == pattern[pIndex] || pattern[pIndex] == '.'){
                return matchCore(str, strIndex+1, pattern, pIndex+1);
            }else{
                return false;
            }
        }
    }

    public static void main(String[] args) {
        RegularMatch test = new RegularMatch();
        String str = "aaa";
        String pattern = "aab*a";

        boolean result = test.match(str.toCharArray(), pattern.toCharArray());
        System.out.println(result);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325609014&siteId=291194637