regular expression matching --python

来源:互联网 发布:epub制作软件安卓版 编辑:程序博客网 时间:2024/06/05 00:20

递归

class Solution(object):    def isMatch(self, s, p):        if not p:            return not s        if len(p) == 1 or p[1] != '*':            return len(s) >0 and (p[0] == '.' or s[0] == p[0])and self.isMatch(s[1:], p[1:])        while s and (p[0] == '.' or s[0] == p[0]):            if self.isMatch(s, p[2:]):                return True            s = s[1:]        return self.isMatch(s, p[2:])    

具体理解参考https://www.cnblogs.com/skysand/p/4292973.html

python自带正则表达式

 def isMatch(self, s, p):          return re.match('^' + p + '$', s) != None  

这应该是网上的两个最简洁的解题思路了。