If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding MatchObject instance. Return None if the string does not match the pattern; note that this is different from a zero-length match.
import re
class MyTest:
a = re.compile('abc')
def testthis(self, fname):
print fname
if self.a.match(fname):
return 'yes'
else:
return 'no'
t = MyTest()
print t.testthis('testabc')
从the docs(我的重点):
也许你想要
.search()
。你知道吗如果要使用regex
abc
并使其匹配testabc
,则必须使用search
而不是match
。match
只匹配字符串开头;search
匹配字符串中的任何位置。你知道吗根据您添加到问题中的注释,您发现为这个python代码打印的值
no
:这让您感到惊讶,因为它在Perl中是匹配的。你知道吗
这是因为在Python中,
match
在字符串的开头工作,不像Perl那样m
在字符串的任何地方查找匹配项。(在Java中,它处理整个字符串)相关问题 更多 >
编程相关推荐