题解 | #正则表达式匹配#
正则表达式匹配
http://www.nowcoder.com/practice/28970c15befb4ff3a264189087b99ad4
int match_core(char* str,char* pattern)
{
if (*str == '\0' && *pattern == '\0')
return 1;
if (*str != '\0' && *pattern == '\0')
return 0;
if(*str == '\0' && *pattern != '\0')
{
if(pattern[1]=='*')
return match_core(str, pattern+2);
else
return 0;
}
if (pattern[1] == '*')
{
if(pattern[0]==str[0]||pattern[0]=='.')
return (match_core(str + 1, pattern)||match_core(str, pattern + 2));
else
return match_core(str, pattern+2);
}
else
{
if ((pattern[0] == '.') || pattern[0] == str[0])
return match_core(str + 1, pattern + 1);
else
return 0;
}
}
bool match(char* str, char* pattern ) {
if (str == NULL || pattern == NULL)
return 0;
return match_core(str, pattern);
}