WildcardMatching和Regex,通配符匹配和正则表达式匹配
WildcardMatching:通配符匹配
算法分析:
1. 二个指针i, j分别指向字符串、匹配公式。
2. 如果匹配,直接2个指针一起前进。
3. 如果匹配公式是*,在字符串中依次匹配即可。
注意记录上一次开始比较的位置
Implement wildcard pattern matching with support for '?'
and '*'
'?' Matches any single character.
'*' Matches any sequence of characters (including the empty sequence).
- Some examples:
- isMatch("aa","a") → false
- isMatch("aa","aa") → true
- isMatch("aaa","aa") → false
- isMatch("aa", "*") → true
- isMatch("aa", "a*") → true
- isMatch("ab", "?*") → true
- isMatch("aab", "c*a*b") → false
- public class WildcardMatching {
- public boolean isMatch(String s, String p)
- {
- if (s == null || p == null)
- {
- return false;
- }
- int indexS = 0;
- int indexP = 0;
- int lenS = s.length();
- int lenP = p.length();
- int preP = -1;//记录通配符为*时通配字符串的下标
- int preS = -1;//记录通配符为*时要匹配的字符串下标
- while (indexS < lenS)
- {
- //非*匹配时,indexS和indexP同时移动
- if (indexP < lenP && (p.charAt(indexP)==s.charAt(indexS)||p.charAt(indexP)=='?'))
- {
- indexP++;
- indexS++;
- }
- //碰到*,记录此时p,s的下标
- else if (indexP < lenP && p.charAt(indexP) == '*')
- {
- preS = indexS;
- preP = indexP;
- // p比较指针后移,假设*匹配空字符串
- indexP++;
- }
- else if (preP != -1)//尝试匹配*
- {
- indexP = preP;//p回退到*位置
- indexP++;
- preS++;//尝试匹配*,每次移动一个字符,然后比较p的*后面字符和s剩下的字符,如果不匹配,回退到*的位置
- indexS = preS;
- }
- else
- {
- return false;
- }
- }
- while (indexP < lenP)
- {
- if (p.charAt(indexP) != '*')
- {
- return false;
- }
- indexP++;
- }
- return true;
- }
- //通过例子aab和*ab来模拟过程
- public static void main(String[] args) {
- WildcardMatching wm = new WildcardMatching();
- System.out.println(wm.isMatch("aab", "*ab"));
- }
- }
Regex:正则表达式匹配
问题描述:Implement regular expression matching with support for '.'
and '*'
.
- '.' 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
算法分析:.*可以匹配任意字符串,例如ab匹配.*,不是说让.匹配完a然后再去匹配*,而是*匹配的是.,也就是说(.*)==(..........),所以.*匹配所有字符串。
利用动态规划,对于匹配字符串p,讨论三种情况,p长度为0,p长度为1,p的长度大于1(p的第二字符串为*,p的第二个字符串不为*)
- //动态规划
- public class Regex2 {
- public boolean isMatch(String s, String p) {
- // p长度为0,边界条件。
- if (p.length() == 0) {
- return s.length() == 0;
- }
- // p长度为1,边界条件。
- if (p.length() == 1) {
- // s长度为0
- if (s.length() < 1) {
- return false;
- }
- //首元素匹配有两种情况
- // 如果p为.则s第一个元素和p一定匹配,如果p的第一个元素和s的第一元素相同,也一定匹配。
- else if ((p.charAt(0) != s.charAt(0)) && (p.charAt(0) != '.')) {
- return false;
- }
- // 否则除了第一个匹配的元素外,比较其他的元素,动态规划的思想。
- else {
- return isMatch(s.substring(1), p.substring(1));
- }
- }
- // p的第二个元素不是*,*代表0个或多个前面的元素
- if (p.charAt(1) != '*')
- {
- if (s.length() < 1)
- {
- return false;
- }
- else if ((p.charAt(0) != s.charAt(0)) && (p.charAt(0) != '.'))
- {
- return false;
- }
- else
- {
- return isMatch(s.substring(1), p.substring(1));
- }
- }
- else //p的第二个元素是*
- {
- //*代表0个前面的元素
- if (isMatch(s, p.substring(2)))
- {
- return true;
- }
- //*代表一个或多个前面的元素
- //遍历s,如果s的i元素等于p的第一个元素,或者p的第一个元素为.,匹配s的i+1和p的第三个元素后的字符串
- for(int i = 0;
- i<s.length() && (s.charAt(i) == p.charAt(0) || (p.charAt(0) == '.'));
- i ++ )
- {
- if(isMatch(s.substring(i + 1), p.substring(2)))
- {
- return true;
- }
- }
- return false;
- }
- }
- public static void main(String[] args)
- {
- Regex2 reg2 = new Regex2();
- System.out.println(reg2.isMatch("aaba", ".*"));
- }
- }
WildcardMatching和Regex,通配符匹配和正则表达式匹配的更多相关文章
- [LeetCode][Facebook面试题] 通配符匹配和正则表达式匹配,题 Wildcard Matching
开篇 通常的匹配分为两类,一种是正则表达式匹配,pattern包含一些关键字,比如'*'的用法是紧跟在pattern的某个字符后,表示这个字符可以出现任意多次(包括0次). 另一种是通配符匹配,我们在 ...
- regex - POSIX 1003.2 正则表达式
DESCRIPTION 正则表达式 (``RE''s), 在 POSIX 1003.2 中定义,包含两种类型:新式 REs (基本上指的是 egrep 使用的那些,1003.2 称其为 ``exten ...
- Regular Expression Matching,regex,正则表达式匹配,利用动态规划
问题描述:Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...
- 修改ZuulHandlerMapping私有变量pathMatcher,重写match方法,使url匹配兼容正则表达式。
一.起源 在mocksever中引入了spring cloud zuul做代理转发,如果请求的url和配置的mock规则匹配(精确匹配和模糊匹配),忽略,不做转发.如果mock配置的url和请求url ...
- js正则表达式验证、匹配数字、匹配字符串、匹配中文、匹配任意字符备忘录
本文转自:91博客 :原文地址:http://www.9191boke.com/235792704.html 正则表达式或“regex”用于匹配字符串的各个部分,下面是我创建正则表达式的备忘录.包括一 ...
- SQL中常用模糊查询的四种匹配模式&&正则表达式
执行数据库查询时,有完整查询和模糊查询之分.一般模糊语句如下:SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式:1.%:表示任意0个或多个字 ...
- .NET正则表达式匹配Silverlight
这是一个.NET正则表达式匹配工具的Silverlight 在页面中加入以下代码就可以了: <"> <param name="source" value ...
- c# 正则表达式 匹配回车
1 "." 匹配除 "\n" 之外的任何单个字符,一般用".*?"匹配不包括回车的任意字符. 2 我们在用正则表达式分析html或者是xml ...
- Java/Js下使用正则表达式匹配嵌套Html标签
转自:http://www.jb51.net/article/24422.htm 以前写过一篇文章讲解如何使用正则表达式完美解决Html嵌套标签的匹配问题(使用正则表达式匹配嵌套Html标签),但是里 ...
随机推荐
- 【文章阅读】Java虚拟机系列学习
总目录: Java虚拟机 - 随笔分类 - 五月的仓颉 - 博客园 http://www.cnblogs.com/xrq730/category/731395.html 已读: Java虚拟机1:什么 ...
- Logon Session Times
How to Get User Logon Session Times from the Event Log To figure out user session time, you’ll first ...
- python基础之类的进阶
一.__setitem__,__getitem,__delitem__ #把对象操作属性模拟成字典的格式 class Foo: def __init__(self,name): self.name=n ...
- 《深入理解Linux内核》阅读笔记 --- Chapter 3 Processes
Process Switching 1.The set of data that must be loaded into the registers before the process resume ...
- Dom4j总结
1.DOM4J简介 DOM4J是 dom4j.org 出品的一个开源 XML 解析包.DOM4J应用于 Java 平台,采用了 Java 集合框架并完全支持 DOM,SAX 和 JAXP. DOM4J ...
- ps 和 grep 查找消除 grep自身查找(转载)
用ps -def | grep查找进程很方便,最后一行总是会grep自己. $ ps -def | grep dragonfly-framework dean 5273 5272 0 15:23 pt ...
- C#设置当前程序通过IE代理服务器上网
注意:以下设置只在当前程序中有效,对IE浏览器无效,且关闭程序后,自动释放代码. using System; using System.Collections.Generic; using Syste ...
- 初识ambari
本文地址:http://www.cnblogs.com/qiaoyihang/p/6290467.html 引用:http://blog.csdn.net/yeruby/article/details ...
- Hadoop的eclipse1.1.2插件的安装和配置
我的集群使用的hadoop版本是hadoop-1.1.2.对应的eclipse版本也是:hadoop-eclipse-plugin-1.1.2_20131021200005 (1)在eclipse的d ...
- 用Tchromium替代webbrowser提交网页表单有关问题
用Tchromium替代webbrowser提交网页表单有关问题 提交表单时,使用js脚本,然后用 chrm.browser.Frame['ff'].ExecuteJavaScript 提交就可以 ...