Implement regular expression matching with support for '.'
and '*'
.
'.' Matches any single character.
'*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be:
bool isMatch(const char *s, const char *p) Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true
正则表达式的匹配,注意*代表的不是任意的字符,而是0个或者任意多个前向的字符,代码如下:
class Solution {
public:
bool isMatch(string s, string p) {
if(p.size() == ) return s.size() == ;
if(p[] != '*'){
if(s.size() != && (p[] == s[] || p[] == '.'))
return isMatch(s.substr(), p.substr());
return false;
}else{
while(s.size() != &&(s[] == p[] || p[] == '.')){//模式串匹配0个或者更多的字符
if(isMatch(s, p.substr()))
return true;
s = s.substr();
}
return isMatch(s, p.substr());
}
}
};