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).

  1. Some examples:
  2. isMatch("aa","a") false
  3. isMatch("aa","aa") true
  4. isMatch("aaa","aa") false
  5. isMatch("aa", "*") true
  6. isMatch("aa", "a*") true
  7. isMatch("ab", "?*") true
  8. isMatch("aab", "c*a*b") false
  1. public class WildcardMatching {
  2. public boolean isMatch(String s, String p)
  3. {
  4. if (s == null || p == null)
  5. {
  6. return false;
  7. }
  8.  
  9. int indexS = 0;
  10. int indexP = 0;
  11.  
  12. int lenS = s.length();
  13. int lenP = p.length();
  14.  
  15. int preP = -1;//记录通配符为*时通配字符串的下标
  16. int preS = -1;//记录通配符为*时要匹配的字符串下标
  17.  
  18. while (indexS < lenS)
  19. {
  20. //非*匹配时,indexS和indexP同时移动
  21. if (indexP < lenP && (p.charAt(indexP)==s.charAt(indexS)||p.charAt(indexP)=='?'))
  22. {
  23. indexP++;
  24. indexS++;
  25. }
  26.  
  27. //碰到*,记录此时p,s的下标
  28. else if (indexP < lenP && p.charAt(indexP) == '*')
  29. {
  30. preS = indexS;
  31. preP = indexP;
  32.  
  33. // p比较指针后移,假设*匹配空字符串
  34. indexP++;
  35. }
  36.  
  37. else if (preP != -1)//尝试匹配*
  38. {
  39. indexP = preP;//p回退到*位置
  40. indexP++;
  41.  
  42. preS++;//尝试匹配*,每次移动一个字符,然后比较p的*后面字符和s剩下的字符,如果不匹配,回退到*的位置
  43. indexS = preS;
  44. }
  45.  
  46. else
  47. {
  48. return false;
  49. }
  50. }
  51.  
  52. while (indexP < lenP)
  53. {
  54. if (p.charAt(indexP) != '*')
  55. {
  56. return false;
  57. }
  58. indexP++;
  59. }
  60.  
  61. return true;
  62. }
  63. //通过例子aab和*ab来模拟过程
  64. public static void main(String[] args) {
  65. WildcardMatching wm = new WildcardMatching();
  66. System.out.println(wm.isMatch("aab", "*ab"));
  67. }
  68. }

Regex:正则表达式匹配

问题描述:Implement regular expression matching with support for '.' and '*'.

  1. '.' Matches any single character.
  2. '*' Matches zero or more of the preceding element.
  3.  
  4. The matching should cover the entire input string (not partial).
  5.  
  6. The function prototype should be:
  7. bool isMatch(const char *s, const char *p)
  8.  
  9. Some examples:
  10. isMatch("aa","a") false
  11. isMatch("aa","aa") true
  12. isMatch("aaa","aa") false
  13. isMatch("aa", "a*") true
  14. isMatch("aa", ".*") true
  15. isMatch("ab", ".*") true
  16. isMatch("aab", "c*a*b") true

算法分析:.*可以匹配任意字符串,例如ab匹配.*,不是说让.匹配完a然后再去匹配*,而是*匹配的是.,也就是说(.*)==(..........),所以.*匹配所有字符串。

利用动态规划,对于匹配字符串p,讨论三种情况,p长度为0,p长度为1,p的长度大于1(p的第二字符串为*,p的第二个字符串不为*)

  1. //动态规划
  2. public class Regex2 {
  3. public boolean isMatch(String s, String p) {
  4. // p长度为0,边界条件。
  5. if (p.length() == 0) {
  6. return s.length() == 0;
  7. }
  8.  
  9. // p长度为1,边界条件。
  10. if (p.length() == 1) {
  11.  
  12. // s长度为0
  13. if (s.length() < 1) {
  14. return false;
  15. }
  16. //首元素匹配有两种情况
  17. // 如果p为.则s第一个元素和p一定匹配,如果p的第一个元素和s的第一元素相同,也一定匹配。
  18. else if ((p.charAt(0) != s.charAt(0)) && (p.charAt(0) != '.')) {
  19. return false;
  20. }
  21.  
  22. // 否则除了第一个匹配的元素外,比较其他的元素,动态规划的思想。
  23. else {
  24. return isMatch(s.substring(1), p.substring(1));
  25. }
  26. }
  27.  
  28. // p的第二个元素不是*,*代表0个或多个前面的元素
  29. if (p.charAt(1) != '*')
  30. {
  31. if (s.length() < 1)
  32. {
  33. return false;
  34. }
  35. else if ((p.charAt(0) != s.charAt(0)) && (p.charAt(0) != '.'))
  36. {
  37. return false;
  38. }
  39. else
  40. {
  41. return isMatch(s.substring(1), p.substring(1));
  42. }
  43. }
  44.  
  45. else //p的第二个元素是*
  46. {
  47. //*代表0个前面的元素
  48. if (isMatch(s, p.substring(2)))
  49. {
  50. return true;
  51. }
  52.  
  53. //*代表一个或多个前面的元素
  54. //遍历s,如果s的i元素等于p的第一个元素,或者p的第一个元素为.,匹配s的i+1和p的第三个元素后的字符串
  55. for(int i = 0;
  56. i<s.length() && (s.charAt(i) == p.charAt(0) || (p.charAt(0) == '.'));
  57. i ++ )
  58. {
  59. if(isMatch(s.substring(i + 1), p.substring(2)))
  60. {
  61. return true;
  62. }
  63. }
  64. return false;
  65. }
  66. }
  67. public static void main(String[] args)
  68. {
  69. Regex2 reg2 = new Regex2();
  70. System.out.println(reg2.isMatch("aaba", ".*"));
  71. }
  72. }

WildcardMatching和Regex,通配符匹配和正则表达式匹配的更多相关文章

  1. [LeetCode][Facebook面试题] 通配符匹配和正则表达式匹配,题 Wildcard Matching

    开篇 通常的匹配分为两类,一种是正则表达式匹配,pattern包含一些关键字,比如'*'的用法是紧跟在pattern的某个字符后,表示这个字符可以出现任意多次(包括0次). 另一种是通配符匹配,我们在 ...

  2. regex - POSIX 1003.2 正则表达式

    DESCRIPTION 正则表达式 (``RE''s), 在 POSIX 1003.2 中定义,包含两种类型:新式 REs (基本上指的是 egrep 使用的那些,1003.2 称其为 ``exten ...

  3. Regular Expression Matching,regex,正则表达式匹配,利用动态规划

    问题描述:Implement regular expression matching with support for '.' and '*'. '.' Matches any single char ...

  4. 修改ZuulHandlerMapping私有变量pathMatcher,重写match方法,使url匹配兼容正则表达式。

    一.起源 在mocksever中引入了spring cloud zuul做代理转发,如果请求的url和配置的mock规则匹配(精确匹配和模糊匹配),忽略,不做转发.如果mock配置的url和请求url ...

  5. js正则表达式验证、匹配数字、匹配字符串、匹配中文、匹配任意字符备忘录

    本文转自:91博客 :原文地址:http://www.9191boke.com/235792704.html 正则表达式或“regex”用于匹配字符串的各个部分,下面是我创建正则表达式的备忘录.包括一 ...

  6. SQL中常用模糊查询的四种匹配模式&&正则表达式

    执行数据库查询时,有完整查询和模糊查询之分.一般模糊语句如下:SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式:1.%:表示任意0个或多个字 ...

  7. .NET正则表达式匹配Silverlight

    这是一个.NET正则表达式匹配工具的Silverlight 在页面中加入以下代码就可以了: <"> <param name="source" value ...

  8. c# 正则表达式 匹配回车

    1 "." 匹配除 "\n" 之外的任何单个字符,一般用".*?"匹配不包括回车的任意字符. 2 我们在用正则表达式分析html或者是xml ...

  9. Java/Js下使用正则表达式匹配嵌套Html标签

    转自:http://www.jb51.net/article/24422.htm 以前写过一篇文章讲解如何使用正则表达式完美解决Html嵌套标签的匹配问题(使用正则表达式匹配嵌套Html标签),但是里 ...

随机推荐

  1. 【文章阅读】Java虚拟机系列学习

    总目录: Java虚拟机 - 随笔分类 - 五月的仓颉 - 博客园 http://www.cnblogs.com/xrq730/category/731395.html 已读: Java虚拟机1:什么 ...

  2. Logon Session Times

    How to Get User Logon Session Times from the Event Log To figure out user session time, you’ll first ...

  3. python基础之类的进阶

    一.__setitem__,__getitem,__delitem__ #把对象操作属性模拟成字典的格式 class Foo: def __init__(self,name): self.name=n ...

  4. 《深入理解Linux内核》阅读笔记 --- Chapter 3 Processes

    Process Switching 1.The set of data that must be loaded into the registers before the process resume ...

  5. Dom4j总结

    1.DOM4J简介 DOM4J是 dom4j.org 出品的一个开源 XML 解析包.DOM4J应用于 Java 平台,采用了 Java 集合框架并完全支持 DOM,SAX 和 JAXP. DOM4J ...

  6. ps 和 grep 查找消除 grep自身查找(转载)

    用ps -def | grep查找进程很方便,最后一行总是会grep自己. $ ps -def | grep dragonfly-framework dean 5273 5272 0 15:23 pt ...

  7. C#设置当前程序通过IE代理服务器上网

    注意:以下设置只在当前程序中有效,对IE浏览器无效,且关闭程序后,自动释放代码. using System; using System.Collections.Generic; using Syste ...

  8. 初识ambari

    本文地址:http://www.cnblogs.com/qiaoyihang/p/6290467.html 引用:http://blog.csdn.net/yeruby/article/details ...

  9. Hadoop的eclipse1.1.2插件的安装和配置

    我的集群使用的hadoop版本是hadoop-1.1.2.对应的eclipse版本也是:hadoop-eclipse-plugin-1.1.2_20131021200005 (1)在eclipse的d ...

  10. 用Tchromium替代webbrowser提交网页表单有关问题

    用Tchromium替代webbrowser提交网页表单有关问题   提交表单时,使用js脚本,然后用 chrm.browser.Frame['ff'].ExecuteJavaScript 提交就可以 ...