*:匹配任意个字符

?:匹配至多1个字符

<?phpclass MNode
{public $strIndex;public $patIndex;public $leftMatch = null;   //精确匹配public $midMatch = null;    //模式匹配public $rightMatch = null;  //不能匹配public function __construct($strIndex, $patIndex){$this->strIndex = $strIndex;$this->patIndex = $patIndex;}
}class RegMatch
{public $root, $match = false;public $string, $pattern;public $strLen, $patLen;public function __construct(string $string, string $pattern){$this->root = new MNode(0, 0);$this->string = $string;$this->pattern = $pattern;$this->strLen = strlen($string);$this->patLen = strlen($pattern);}public function isMatch(){return $this->doMatch($this->root);}protected function doMatch($root){static $matchCount = 0;if (empty($root) || empty($this->string) || empty($this->pattern) || $this->match == true|| $root->strIndex >= $this->strLen || $root->patIndex >= $this->patLen) {if ($root->patIndex >= $this->patLen) {$this->match = true;}return $this->match;}if ($this->string[$root->strIndex] == $this->pattern[$root->patIndex]) {$root->leftMatch = new MNode($root->strIndex + 1, $root->patIndex + 1);$this->doMatch($root->leftMatch);}//模式情况if ($this->pattern[$root->patIndex] == '*') {$root->midMatch = new MNode($root->strIndex + 1, $root->patIndex + 1);$this->doMatch($root->midMatch);if ($this->match == false) {$root->rightMatch = new MNode($root->strIndex, $root->patIndex + 1);$this->doMatch($root->rightMatch);}}if ($this->pattern[$root->patIndex] == '?') {$root->rightMatch = new MNode($root->strIndex, $root->patIndex + 1);$this->doMatch($root->rightMatch);if ($this->match == false && $matchCount <= 1) {$matchCount++;$root->midMatch = new MNode($root->strIndex + 1, $root->patIndex + 1);$this->doMatch($root->midMatch);}}if ($this->match == false && $root->patIndex == 0) {$root->rightMatch = new MNode($root->strIndex + 1, $root->patIndex);$this->doMatch($root->rightMatch);}return $this->match;}
}$obj = new RegMatch('abc', '*c');echo $obj->isMatch();

转载于:https://www.cnblogs.com/SHQHDMR/p/11184242.html

回溯法实现正则匹配判断相关推荐

  1. 正则匹配判断电话号码是否真实、几连号、几顺序号

    正则匹配判断电话号码是否真实.几连号.几顺序号 正则匹配 1.主要是从输入的字符串中匹配出满足条件的字符串,即判断是否为电话号码. 2.电话号码的连号和顺序号判断,主要是通过正则匹配式将相应的字符串匹 ...

  2. 微信小程序:正则匹配判断电话号码

    1.js // formSubmit 表单提交formSubmit:function(e){console.log('form表单发生点击事件,携带的数据为:',e.detail.value)cons ...

  3. Python正则匹配判断手机号是否合法

    正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达式.常规表示法(英语:Regular Expression,在代码中常简写为regex.regexp或RE),是计算机科学的一个概念.正则表 ...

  4. python判断电话号码是否合理_Python正则匹配判断手机号是否合法的方法

    正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达式.常规表示法(英语:Regular Expression,在代码中常简写为regex.regexp或RE),是计算机科学的一个概念.正则表 ...

  5. 回溯法之避免无用判断 UVA129 Krypton Factor困难的串

    题意:如果一个字符串包含两个相邻的重复子串,则称它是"容易的串",其他串称为"困难的串" 输入正整数n和L,输出由前L个字符组成的并且它的字典序是第n小的串,和 ...

  6. 子集和数问题——回溯法(C++)

    问题描述 已知(w1, w2, -, wn)和M,均为正数.要求找出wi的和数等于M的所有子集. 例如:若n=4,(w1,w2,w3,w4)=(11,13,24,7),M=31,则满足要求的子集是(1 ...

  7. 14. Perl 正则表达式-正则匹配

    正则表达式(regular expression) 在Perl 里面通常也叫模式(Pattern). 正则表达式是用来匹配或不匹配某个字符串是否符合特定字符串模板的,也可以实现字符串的替换. 1. 正 ...

  8. php 正则 回溯,PHP正则匹配绕过

    之前没有从机制上去了解过PHP正则匹配绕过具体是怎么一回事,于是主动去网上找了一些资料来加深理解 NFA与正则表达式 常见的正则引擎,被细分为DFA(确定性有限状态自动机)与NFA(非确定性有限状态自 ...

  9. PHP正则匹配效率,PHP 正则表达式效率 贪婪、非贪婪与回溯分析(推荐)

    先扫盲一下什么是正则表达式的贪婪,什么是非贪婪?或者说什么是匹配优先量词,什么是忽略优先量词? 好吧,我也不知道概念是什么,来举个例子吧. 某同学想过滤之间的内容,那是这么写正则以及程序的.$str ...

最新文章

  1. [转]你不需要jQuery
  2. pyppeteer(python版puppeteer)基本使用
  3. java 检测ip网速_java心跳测网速Demo
  4. LeetCode MySQL 580. 统计各专业学生人数
  5. String.valueOf() 和 toString的区别
  6. 我们注意到您的计算机目前处于离线状态_你为什么会选择用反渗透设备离线清洗设备?...
  7. 数组——询问学号(洛谷 P3156)
  8. 如何使用Java代码获取Android移动终端Mac地址
  9. python创建二维空列表_python创建与遍历List二维列表的方法
  10. 网吧服务器记录修改,网吧服务器ip地址修改
  11. 堆栈平衡:估计这是最详细的讲解堆栈平衡的了
  12. 2022-2028年中国饮用水行业市场专项调研及投资前景研究报告
  13. Ingest Node Pipeline Processor
  14. <C和指针>---生存期和存储类型
  15. linux下删除隐藏文件夹及子文件夹
  16. java poi row cell,使用POI进行Excel操作的总结一——创建Workbook,Sheet,Row以及Cell
  17. Unity TilePalette中素材图片与Tile里Cell大小关系
  18. Arm内核的Oops错误定位方法
  19. shell函数(用法及定义)
  20. m基于强化学习的PID控制器simulink仿真,对比PI控制器和变结构PI控制器

热门文章

  1. ActionScript 3操作XML 详解
  2. 使用ASP.NET Atlas编写显示真实进度的ProgressBar(进度条)控件
  3. 小程序前端性能测试_如何提高前端应用程序的性能
  4. esl8266开发之旅_从ESL老师到越南软件开发人员的旅程
  5. 谈论源码_6,000名自由职业者谈论金​​钱,幸福和对未来的希望
  6. javascript_JavaScript疲劳疲劳
  7. (C++)判断一个序列是non-increasing/non-decreasing还是两者都不的两个方法
  8. 分享五款java学习辅助工具,总有你用的上的~
  9. Azure编配器简化有状态无服务器工作流的创建
  10. OCQ亮相中国移动办公峰会 荣获2017中国移动办公创新品牌