(Java refers to offer) Regular expression matching

1. Question analysis

Please implement a function to match regular expressions including '.'and '*'.
The character in the pattern '.'represents any character, and '*'the character before it can appear any number of times (including 0 times).

In this question, matching means that all characters of the string match the entire pattern. For example, the string "aaa" and the pattern "a.a"and "ab*ac*a"match, but with "aa.a"and "ab*a"do not match

This question is mainly to determine whether the second character in the pattern*
Based on this, we will discuss the situation in detail:
(1) When the second character in the pattern is not *:

If the first character of the string matches the first character in the pattern, then both the string and the pattern are shifted back by one character, and then the remaining characters are matched.
If the first character of the string does not match the first character in the pattern, Return false directly

(2) When the second character in the pattern is *:

If the first character of the string does not match the first character of the pattern, the pattern is shifted by 2 characters and continue to match.
If the first character of the string matches the first character of the pattern, there are 3 matching methods:

  • After the mode shift two characters, the equivalent of X*being ignored
  • The string is moved backward by 1 character, and the pattern is moved backward by 2 characters
  • The string is moved back by one character, and the pattern remains unchanged, then the next character will continue to be matched, because * can match multiple digits

Second, the code

/**
 * @Auther: Yolo
 * @Date: 2020/9/10 08:49
 * @Description:
 */
public class Test_10 {
    
    
    public static void main(String[] args) {
    
    
        char[] str = {
    
    };
        char[] pattern = {
    
    };
        boolean result = match(str, pattern);
        System.out.println(result);
    }

    private static boolean match(char[] str, char[] pattern) {
    
    
        if (str == null || pattern == null) {
    
    
            return false;
        }
        int strIndex = 0;
        int patternIndex = 0;
        return matchCore(str, strIndex, pattern, patternIndex);
    }

    private static boolean matchCore(char[] str, int strIndex, char[] pattern, int patternIndex) {
    
    
        //有效性检验,str 到尾,pattern 到尾匹配成功
        if (strIndex == str.length && patternIndex == pattern.length) {
    
    
            return true;
        }
        //pattern 先到尾,匹配失败
        if (strIndex != str.length && patternIndex == pattern.length) {
    
    
            return false;
        }
        //模式第二个是 '*',且字符串第一个跟模式第一个匹配,分三种匹配;如果不匹配,模式后移两位
        if (patternIndex + 1 < pattern.length && pattern[patternIndex + 1] == '*') {
    
    
            if (strIndex != str.length && (pattern[patternIndex] == str[strIndex] || pattern[patternIndex] == '.')) {
    
    

                return matchCore(str, strIndex, pattern, patternIndex + 2)
                        || matchCore(str, strIndex + 1, pattern, patternIndex + 2)
                        || matchCore(str, strIndex + 1, pattern, patternIndex);
            } else {
    
    
                //模式后移两位,相当于 x* 被忽略
                return matchCore(str, strIndex, pattern, patternIndex + 2);
            }
        }
        //模式第二个不是 '*',且字符串第一个跟模式第一个匹配,则都后移一维,否则直接返回 false
        if (strIndex != str.length && (pattern[patternIndex] == str[strIndex] || pattern[patternIndex] == '.')) {
    
    
            return matchCore(str, strIndex + 1, pattern, patternIndex + 1);
        }
        return false;
    }
}

Three, summary

The main question is the second *process and the subsequent determination of

Guess you like

Origin blog.csdn.net/nanhuaibeian/article/details/108506603