【Leetcode/python】10. Regular Expression Matching

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
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,160评论 0 10
  • 三顺妮子阅读 1,332评论 0 0
  • 我还是很想你,我还是很爱你
    看起来像花阅读 1,625评论 0 0
  • 6.11感恩日记 感恩新的一周又开始了,大家各奔东西奔赴各自的岗位开始了新的忙碌! 感恩父母的养育之恩,父母不仅把...
    种子高手阅读 676评论 0 0
  • 从2017年,我高二,有了高三的备考意识的那个时候,到高考的那段时间大概一年有有余。而现在,距离高考已经过去了两个...
    公仔碗仔阅读 1,501评论 0 0