使用re.search进行Python精确模式匹配
我想在文件上搜索stop_enabc的精确搜索,但如果文件有此stop_enabc,我的代码也会返回stop_enabc使用re.search进行Python精确模式匹配,python,search,Python,Search,我想在文件上搜索stop_enabc的精确搜索,但如果文件有此stop_enabc,我的代码也会返回stop_enabc 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, lin
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
你是说这个
re.search(r"^stop_enabc$", line)
提供示例输入文件..和预期输出