zoukankan      html  css  js  c++  java
  • 正则表达式匹配

    实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配。

     1 namespace JianZhiOffer
     2 {
     3     class RegexMatch
     4     {
     5         /// <summary>
     6         /// 取自牛客网解法
     7         /// </summary>
     8         /// <param name="str"></param>
     9         /// <param name="pattern"></param>
    10         /// <returns></returns>
    11         public bool match(char[] str, char[] pattern)
    12         {
    13             // 如果为空, 返回 false
    14             if (str == null || pattern == null)
    15                 return false;
    16 
    17             // 从第一个字符开始
    18             int strIndex = 0;
    19             int patternIndex = 0;
    20             return matchRegex(str, strIndex, pattern, patternIndex);
    21         }
    22 
    23         private bool matchRegex(char[] str, int strIndex, char[] pattern, int patternIndex)
    24         {
    25             // 递归, 退出条件, 字符和模式同时到达结尾
    26             if (strIndex == str.Length && patternIndex == pattern.Length)
    27                 return true;
    28 
    29             // 退出条件, 模式结束,字符还没结束
    30             if (strIndex < str.Length && patternIndex == pattern.Length)
    31                 return false;
    32 
    33             // 模式第二个字符是‘*’
    34             if (patternIndex + 1 < pattern.Length && pattern[patternIndex + 1] == '*')
    35             {
    36                 if ((strIndex != str.Length && pattern[patternIndex] == str[strIndex]) 
    37                     || (pattern[patternIndex] == '.' && strIndex != str.Length))
    38                 {
    39                     return matchRegex(str, strIndex, pattern, patternIndex + 2) // 视为x*匹配0个字符,模式后移2
    40                         || matchRegex(str, strIndex + 1, pattern, patternIndex + 2) // 视为模式匹配1个字符
    41                         || matchRegex(str, strIndex + 1, pattern, patternIndex); // *匹配1个,匹配str中的下一个
    42                 }
    43                 else
    44                 {
    45                     return matchRegex(str, strIndex, pattern, patternIndex + 2);
    46                 }
    47             }
    48 
    49             // 模式第2个字符不是*,且字符串第1个跟模式第1个匹配,则都后移1位,否则直接返回false
    50             if ((strIndex != str.Length && pattern[patternIndex] == str[strIndex]) 
    51                 || (pattern[patternIndex] == '.' && strIndex != str.Length))
    52             {
    53                 return matchRegex(str, strIndex + 1, pattern, patternIndex + 1);
    54             }
    55 
    56             return false;
    57         }
    58     }
    59 }
  • 相关阅读:
    AngularJS中$http服务的简单用法
    js变量作用域及访问权限的探讨(2)
    javascript中类的属性访问权限研究(1)
    zoom:1-hasLayout
    JS 继承(类式 与 原型式)
    迷你版jQuery——zepto核心源码分析
    移动互联网终端的touch事件,touchstart, touchend, touchmove
    WEBAPP开发技巧总结
    JQuery flot API文档 中文版
    jQuery插件Flot实战Demo
  • 原文地址:https://www.cnblogs.com/xiaolongren/p/12003830.html
Copyright © 2011-2022 走看看