脚本 :
import re
matches = [ hello , hey , hi , hiya ]
def check_match(string):
for item in matches:
if re.search(item, string):
print Match found: + string
else:
print Match not found: + string
check_match( hey )
check_match( hello there )
check_match( this should not match )
check_match( oh, hiya )
产出:
Match not found: hey
Match found: hey
Match not found: hey
Match not found: hey
Match found: hello there
Match not found: hello there
Match not found: hello there
Match not found: hello there
Match not found: this should not match
Match not found: this should not match
Match found: this should not match
Match not found: this should not match
Match not found: oh, hiya
Match not found: oh, hiya
Match found: oh, hiya
Match found: oh, hiya
有些事情我不明白。 首先,每个字符串在这个输出中被搜索了四次,有些返回两个作为发现匹配,有些返回三个。我不确定导致这种情况的代码有什么问题,但有人能尝试看出什么问题吗?
预期产出如下:
Match found: hey
Match found: hello there
Match not found: this should not match
Match found: oh, hiya