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

#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

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...