加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 06:34:58 所属栏目:百科 来源:网络整理
导读:请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"a
请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配
#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 == '' &&*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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读