Python精确模式匹配使用检索

2024-10-03 15:27:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我想搜索文件上的stop\u enabc的精确搜索,但如果文件有这个stop\u enabcc,我的代码也会返回stop\u 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

Tags: 文件代码testtxtsearchdeflineresult