在正则表达式python中找到一行以" - "结尾

fyr*_*r91 1 python regex

我试图在文本文件中找到以" - "结尾的行.我使用了以下表达式但没有工作.我不熟悉正则表达式.有人能帮我吗?谢谢!

if re.match(r'[.*+]+\-+[\r\n]', lines[i]):
    return i
Run Code Online (Sandbox Code Playgroud)

the*_*eye 5

re.match只有在从字符串的开头找到匹配项时才会匹配字符串.如果您真的想使用re.match,可以使用以下正则表达式

if re.match(r'.*-$', lines[i].rstrip("\n")):
    return i
Run Code Online (Sandbox Code Playgroud)

但是你根本不需要正则表达式,你可以做这样的事情

for i, line in enumerate(lines):
    if line.rstrip("\n")[-1] == "-":
       return i 
Run Code Online (Sandbox Code Playgroud)