Regular Expression Matching

来源:互联网 发布:温州动车事故真相知乎 编辑:程序博客网 时间:2024/06/08 05:51

首先明确一点s中是没有 * 和 . 的,只有正常字符,很久才弄清楚这一点,不然这题很难理解


//s 待匹配字符串,只有普通的字符,没有*, .
    //p 匹配模板
    bool isMatch(const char *s, const char *p) {
       

  1. if (*p == '\0'return *s == '\0';  
  2.   
  3.     // next char is not '*': must match current character  
  4.     if (*(p+1) != '*'
  5.         return ((*p == *s) || (*p == '.' && *s != '\0')) && isMatch(s+1, p+1);  
  6.       
  7.     // next char is '*'  
  8.     while ((*p == *s) || (*p == '.' && *s != '\0')) {  
  9.         if (isMatch(s, p+2)) return true;  
  10.         s++;  
  11.     }  
  12.     return isMatch(s, p+2);  

    }

0 0
原创粉丝点击