前端之家收集整理的这篇文章主要介绍了
【剑指offer】题53:正则表达式匹配,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
bool match_core(char* str,char * pattern)
{
if (*str == '\0'&&*pattern == '\0')
{
return true;
}
if (*str != '\0' && *pattern == '\0')
{
return false;
}
if (*(pattern+1)=='*')
{
if ((*pattern == *str)||(*pattern=='.'&&*str!='\0'))
{
return match_core(str + 1,pattern + 2)
|| match_core(str + 1,pattern)
|| match_core(str,pattern + 2);
}
else
{
return match_core(str,pattern + 2);
}
}
if ((*str == *pattern) || (*pattern == '.'&&*str != '\0'))
{
return match_core(str + 1,pattern + 1);
}
return false;
}
bool match(char* str,char* pattern)
{
if (str==NULL||pattern==NULL)
{
return false;
}
return match_core(str,pattern);
}
原文链接:https://www.f2er.com/regex/358209.html