请实现一个函数用来匹配包括'.'和'*'的正则表达式

前端之家收集整理的这篇文章主要介绍了请实现一个函数用来匹配包括'.'和'*'的正则表达式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#include "stdafx.h"
#include<iostream> #include<vector> #include<string> #include<queue> #include<stack> #include<cstring> #include<string.h> #include<deque> #include <forward_list>

using namespace std ;

//关于能否匹配可用递归的方式实现
//匹配上的情况

//1.下一位是*,分三种情况:
//1.1 matchCore(str+1,pattern) 模式串匹配成功,并尝试匹配下一字符

//1.3 matchCore(str,pattern+2) 模式串未匹配 //2.下一位不是*,则pattern对应为应该与str相等或者pattern的对应为为. //matchCore(str+1,pattern + 1) //3.对应为不匹配,返回false
class Solution {
public :
bool match(char* str,char* pattern)
{
if (str == NULL || pattern == )
return false ;
matchCore(str,pattern);
}
matchCore( (*str == '\0' &&*pattern == ) true ; //迭代终止条件
(*str != ; (*(pattern + 1 ) == '*' )
{
(*str == *pattern||(*pattern== '.' &&*str!= ))
matchCore(str + ,pattern) || matchCore(str,pattern + 2 );
else );
}
(*str == *pattern || (*pattern== matchCore(str+ );

;
}
};
int main()
{


Solution so;
char * str = "aaa" ;
//char* pattern1 = "a*"; // cout << "str的长度是:" << strlen(str) << endl; * pattern1 = "ab*a*c*a" * pattern2 = "a.a" * pattern3 = "ab*a" * pattern4 = "aa.a" * pattern5 = "bbbba" * pattern6 = ".*a*a" /*char* str = "aaa";
char* pattern1 = "bbbba";*/
//char* pattern1 = ""; //
cout << "pattern1 匹配的结果是: " << so.match(str,pattern1) << endl ;
"pattern2 匹配的结果是: " "pattern3 匹配的结果是: " "pattern4 匹配的结果是: " "pattern5 匹配的结果是: " << so.match(pattern5,pattern6) << 0 ;
}
转自 https://www.cnblogs.com/Czc963239044/p/6973831.html

猜你在找的正则表达式相关文章