Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用re.search进行Python精确模式匹配_Python_Search - Fatal编程技术网

使用re.search进行Python精确模式匹配

使用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

我想在文件上搜索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, line):
           return line
输入样本:

test.txt

stop_enabc

xxxxxxxxx
yyyyyyyyyystop_enabccc
stop_enabccc
你是说这个

re.search(r"^stop_enabc$", line)

提供示例输入文件..和预期输出