【2017.12.10】10. Regular Expression Matching 正则表达式匹配
来源:互联网 发布:光明数据 编辑:程序博客网 时间:2024/05/15 23:48
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") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "a*") → trueisMatch("aa", ".*") → trueisMatch("ab", ".*") → trueisMatch("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-解题
- 【2017.12.10】10. Regular Expression Matching 正则表达式匹配
- 10. Regular Expression Matching&正则表达式匹配
- Regular Expression Matching 正则表达式匹配
- Regular Expression Matching正则表达式匹配
- 正则表达式匹配-leetcode Regular Expression Matching
- 正则表达式匹配(Regular Expression Matching)
- LeetCode 10. Regular Expression Matching(正则表达式匹配)
- LeetCode 10 Regular Expression Matching (正则表达式匹配)
- Leetcode #10 Regular Expression Matching 正则表达式匹配 解题小节
- leetcode(10) - Regular Expression Matching 正则表达式匹配
- leetcode 10 Regular Expression Matching(简单正则表达式匹配)
- LeetCode 10 Regular Expression Matching 正则匹配
- regular expression matching 正则匹配
- *LeetCode 10 Regular Expression Matching 正则表达式
- 简单的正则表达式匹配 Regular Expression Matching
- 多正则表达式匹配(Multiple Regular Expression Matching)
- 判断正则表达式是否匹配 Regular Expression Matching @LeetCode
- LeetCode OJ 之 Regular Expression Matching (正则表达式匹配)
- spark源码打包编译步骤
- 编译C程序的流程
- 校赛感想..
- 开发板设置NFS挂载文件系统
- python历史简介
- 【2017.12.10】10. Regular Expression Matching 正则表达式匹配
- Python编程从入门到实践:习题9-1~9-3
- 常见数据库注入
- 视图
- 图片的轮播
- log4j输出日志到flume
- PTA(数据结构)-带头结点的单链表就地逆置
- 利用Mysql5.7的新特性实现多机房高可用架构
- c++ trim 去字符串空格