正则表达式匹配

10 篇文章 0 订阅

问题描述

请实现一个函数用来匹配包含" . . ." 和的" ∗ * " 的正则表达式。模式中的字符" . . ."表示任意一个字符,而 " ∗ * "表示它前面的字符可以出现任意次(含0次)。在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"abaca"匹配,但与"aa.a"及"ab*a"均不匹配。

问题分析

每次从字符串里拿出一个字符和模式串去匹配,那么如何去匹配一个字符?
如果模式中的字符ch不是" . . .",而字符串中的字符也是ch,那么它们互相匹配。如果模式串中的字符ch是" . . ."时,那么可以匹配字符串中的任意一个字符。当字符串中的字符和模式串中的字符相匹配的时候,继续匹配后面的字符。

当模式中的第二个字符是" ∗ * “时,情况就比较复杂。这种情况下,有三种可能的匹配情况:
1.模式向后移动两个字符,这种情况相当于” ∗ * “和它前面的字符被忽略掉了,因为” ∗ * "可以匹配字符串中的0个字符。
2.如果字符串和模式串的第一个字符相匹配,那么字符串向后移动一个字符。模式串可以分为两种情况:向后移动两个字符,或者保持不变。

如下图所示ba*ab的模式串。当匹配进入状态2,并且字符串的字符是a时,有两种选择:进入状态3(在模式上向后移动两个字符),也可以回到状态2(模式保持不变)。
模式ba*ab的非确定有限状态机

程序的终止条件

当字符串和模式串都匹配到字符串结束,那么匹配成功。
当模式串匹配结束,但是字符串没有匹配完,那么匹配失败。

代码

#include <iostream>
#include <cstdio>
using namespace std;

bool matchCore(const char* str, const char* pattern);
bool match(const char* str, const char* pattern)
{
    if (str == nullptr || pattern == nullptr)
        return false;
    return matchCore(str, pattern);
}
bool matchCore(const char* str, const char* pattern)
{
    if (*str != '\0' && *pattern == '\0')
        return false;
    if (*str == '\0' && *pattern == '\0')
        return true;

    if (*(pattern + 1) == '*')
    {
        if (*str == *pattern || (*pattern == '.' && *str != '\0'))
            return matchCore(str+1, pattern)
                || matchCore(str+1, pattern+2)
                || matchCore(str, pattern+2);
        else
            return matchCore(str, pattern+2);
    }
    else if (*str == *pattern || (*pattern == '.' && *str != '\0'))
    {
        return matchCore(str+1, pattern+1);
    }
    return false;
}
// ====================测试代码====================
void Test(const char* testName, const char* string, const char* pattern, bool expected)
{
    if(testName != nullptr)
        printf("%s begins: ", testName);

    if(match(string, pattern) == expected)
        printf("Passed.\n");
    else
        printf("FAILED.\n");
}

int main(int argc, char* argv[])
{
    Test("Test01", "", "", true);
    Test("Test02", "", ".*", true);
    Test("Test03", "", ".", false);
    Test("Test04", "", "c*", true);
    Test("Test05", "a", ".*", true);
    Test("Test06", "a", "a.", false);
    Test("Test07", "a", "", false);
    Test("Test08", "a", ".", true);
    Test("Test09", "a", "ab*", true);
    Test("Test10", "a", "ab*a", false);
    Test("Test11", "aa", "aa", true);
    Test("Test12", "aa", "a*", true);
    Test("Test13", "aa", ".*", true);
    Test("Test14", "aa", ".", false);
    Test("Test15", "ab", ".*", true);
    Test("Test16", "ab", ".*", true);
    Test("Test17", "aaa", "aa*", true);
    Test("Test18", "aaa", "aa.a", false);
    Test("Test19", "aaa", "a.a", true);
    Test("Test20", "aaa", ".a", false);
    Test("Test21", "aaa", "a*a", true);
    Test("Test22", "aaa", "ab*a", false);
    Test("Test23", "aaa", "ab*ac*a", true);
    Test("Test24", "aaa", "ab*a*c*a", true);
    Test("Test25", "aaa", ".*", true);
    Test("Test26", "aab", "c*a*b", true);
    Test("Test27", "aaca", "ab*a*c*a", true);
    Test("Test28", "aaba", "ab*a*c*a", false);
    Test("Test29", "bbbba", ".*a*a", true);
    Test("Test30", "bcbbabab", ".*a*a", false);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值