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
网友评论