我想在文件上搜索stop_enabc的精确搜索,但是如果文件具有该stop_enabcc,我的代码也会返回stop_enabcc
search_term ="stop_enabc"
f = "C:/Users/test.txt"
result=search(f,search)
print result
def search(f,search):
for line in open(f, 'r'):
if re.search(search, line):
return line
输入示例:
test.txt
stop_enabc
xxxxxxxxx
yyyyyyyyyystop_enabccc
stop_enabccc
答案 0 :(得分:0)
你是这个意思吗?
re.search(r"^stop_enabc$", line)