【2017.12.10】10. Regular Expression Matching 正则表达式匹配

10.1 什么是正则表达式 Rugular Expression Matching

正则表达式,又称规则表达式。(英语:Regular Expression,在代码中常简写为regex、regexp或RE),计算机科学的一个概念。正则表通常被用来检索、替换那些符合某个模式(规则)的文本。

给定一个正则表达式和另一个字符串,我们可以达到如下的目的:
1. 给定的字符串是否符合正则表达式的过滤逻辑(称作“匹配”);
2. 可以通过正则表达式,从字符串中获取我们想要的特定部分。

[编译原理]
正则引擎主要可以分为两大类:一种是DFA,一种是NFA。

10.2 The function prototype 函数原型

10.3 正则表达式 ISMatch和Match的区别?

IsMatch指示 Regex 构造函数中指定的正则表达式在指定的输入字符串中是否找到了匹配项。返回的是布尔值。
Match在指定的输入字符串中搜索 Regex 构造函数中指定的正则表达式的第一个匹配项。返回的是包含匹配信息的Match对象。

————-—————————————————分割线啦——————————————————

10. Regular Expression Matching 正则表达式匹配

Implement regular expression matching with support for ‘.’ and ‘*’.
实现正则表达式与支持匹配 ‘.’ he ‘*’

'.' Matches any single character.
'.' 匹配任何单个字符
'*' Matches zero or more of the preceding element.
'*'匹配零个或者多个前面的元素
The matching should cover the entire input string (not partial).
匹配应该覆盖哼歌输入字符串(不是部分的)

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "a*") → true
isMatch("aa", ".*") → true
isMatch("ab", ".*") → true
isMatch("aab", "c*a*b") → true

Solution

这里面最复杂的操作是“”,有一点特别的是“”不会单独出现,他一定是和前面的一个字幕或者 ‘.’ 配成一对。堪称一对”X*”后,他的性质就是:要么匹配0个,要不匹配连续的”X“
题目的关键就是把这一对放到适合的位置。

Approach #1: Recursion (回溯)[Accepted]

Intuition

If there were no Kleene stars (the * wildcard character for regular expressions), the problem would be easier - we simply check from left to right if each character of the text matches the pattern.

When a star is present, we may need to check many different suffixes of the text and see if they match the rest of the pattern. A recursive solution is a straightforward way to represent this relationship.
当星星出现时,我们可能需要检查文本的许多不同的后缀,看它们是否与模式的其余部分匹配。递归解决方案是一种直接的方式来表达这种关系

如果 ‘*’ 出现图案中,我们可能会忽略这部分模式,或者删除文本中的匹配字符。如果我们在这些操作之后剩余的字符串匹配,则匹配初始输入

如果“”不好判断,那我大不了就来个暴力的算法,把“”的所有可能性都测试一遍看是否有满足的,用两个指针i,j来表明当前s和p的字符。
我们采用从后往前匹配,为什么这么匹配,因为如果我们从前往后匹配,每个字符我们都得判断是否后面跟着“”,而且还要考虑越界的问题。但是从后往前没这个问题,一旦遇到“”,前面必然有个字符。

Code:

class Solution {
    public boolean isMatch(String s, String p) {
        //判断 正则表达式 是否为空
        if(p.isEmpty()) return s.isEmpty();

        //字符串 s 不为空的条件下,第一个字符和正则表达式 相同 或者 匹配为 '.' (单个字符)
        boolean first_match = ( !s.isEmpty() && (p.charAt(0) == s.charAt(0) || p.charAt(0) == '.'));

        //正则表达长度大于2 并且第二个字符为 '*'
        if(p.length() >=2 && p.charAt(1) == '*'){
            return (isMatch(s , p.substring(2)) || (first_match && isMatch(s.substring(1),p)));
        }
        else{
            return (first_match && isMatch(s.substring(1),p.substring(1)));
        }
    }
}

————————————————————————分割线——————————————————————————

友情链接:

csdn-解题

leecode-解题

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值