为什么我找不到匹配的?
>>> ti = "abcd"
>>> tq = "abcdef"
>>> check_abcd = re.compile('^abcd')
>>> if check_abcd.search(ti) is check_abcd.search(tq):
... print "Matching"
... else:
... print "not matching"
...
not matching即使两个变量ti和tq是匹配的,并且具有相同的引用
>>> print check_abcd.search(ti)
<_sre.SRE_Match object at 0x7ffbb05559f0>
>>> print check_abcd.search(tq)
<_sre.SRE_Match object at 0x7ffbb05559f0>为什么不匹配呢?
发布于 2015-03-10 09:05:38
`is` is identity testing, == is equality testing.
is will return True if two variables point to the same object, == if the objects referred to by the variables are equal.您可能想要匹配values而不是objects.So,您可以使用
ti = "abcd"
tq = "abcdef"
check_abcd = re.compile('^abcd')
if check_abcd.search(ti).group(0) == check_abcd.search(tq).group(0):
print "Matching"
else:
print "not matching"https://stackoverflow.com/questions/28959371
复制相似问题