[英]Regex to match the line that ends with the exact word rather than similar words
I am trying to parse a config file and display the output on a web UI. 我正在尝试解析配置文件并在Web UI上显示输出。 However, some of the regex I have written returns extra results.
但是,我编写的某些正则表达式会返回额外的结果。 For instance,
例如,
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
This returns the result but also results in the extra output. 这将返回结果,但也会产生额外的输出。 For example,
例如,
bind lb vserver lb-vs-pr-443-v1-abhishek lb-sg-pr-443-v1-abhishek 绑定lb虚拟服务器lb-vs-pr-443-v1-abhishek lb-sg-pr-443-v1-abhishek
bind lb vserver lb-vs-pr-443-v1-abhishek-proxy lb-sg-pr-443-v1-abhishek-proxy 绑定lb vserver lb-vs-pr-443-v1-abhishek-proxy lb-sg-pr-443-v1-abhishek-proxy
It should have returned only the 1st record till abhishek but it also returns abhishek-proxy 它应该只返回第一个记录直到abhishek,但它也返回abhishek-proxy
How should I restrict this? 我应该如何限制呢?
Kindly provide suggestion on the same. 请就此提出建议。
If you want matches that end with 'abhishek'
, you can try this: 如果要以
'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.