美文网首页
【Leetcode/python】10. Regular Exp

【Leetcode/python】10. Regular Exp

作者: FLYNNNOTES | 来源:发表于2018-10-16 22:50 被阅读0次

    Given an input string (s) and a pattern (p), 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).

    Note:

    s could be empty and contains only lowercase letters a-z.
    p could be empty and contains only lowercase letters a-z, and characters like . or *.

    Python 中直接用re模块,大杀器,关于.*的用法,此处于re模块中相同。

    re模块

    • re.match(p, s): 找出匹配的字符串

    match对象

    • .start(): 匹配的起始位置
    • .end(): 匹配的终点位置
    • .span(): 返回( .start(), .end())
    • .group(): 匹配的字符串
    class Solution:
        def isMatch(self, s, p):
            """
            :type s: str
            :type p: str
            :rtype: bool
            """
            res = re.match(p, s)
            # need this if, if not, u will get an error: 'NoneType' object has no attribute 'group'
            if res == None:
                return False
            else:
                return res.group() == s
    

    相关文章

      网友评论

          本文标题:【Leetcode/python】10. Regular Exp

          本文链接:https://www.haomeiwen.com/subject/rjvkzftx.html