繁体   English   中英

正则表达式以匹配以确切单词而不是相似单词结尾的行

[英]Regex to match the line that ends with the exact word rather than similar words

我正在尝试解析配置文件并在Web UI上显示输出。 但是,我编写的某些正则表达式会返回额外的结果。 例如,

lbvs = lb-vs-pr-443-v1-abhishek
lb_vserver_binding = '^bind\s+lb\s+vserver\s+%s\s+([^\s+]+)' %(lbvs)
for line in lb_file_memory:
        if re.match(lb_vserver_binding, line):
            grouped_data = re.search(lb_vserver_binding, line).groups()
            data = grouped_data[0]
            return data

这将返回结果,但也会产生额外的输出。 例如,

绑定lb虚拟服务器lb-vs-pr-443-v1-abhishek lb-sg-pr-443-v1-abhishek

绑定lb vserver lb-vs-pr-443-v1-abhishek-proxy lb-sg-pr-443-v1-abhishek-proxy

它应该只返回第一个记录直到abhishek,但它也返回abhishek-proxy

我应该如何限制呢?

请就此提出建议。

如果要以'abhishek'结尾的匹配项,可以尝试以下操作:

import re
final_list = [line for line in lb_file_memory if re.findall("abhishek$", line)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM