简体   繁体   English

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

[英]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.

相关问题 我希望我的搜索 function 能够在我的数据库中获取多个单词而不是一个单词,或者精确的句子匹配 - I want my search function to be able to take multiple words rather than just one word, or exact sentence match in my database Python正则表达式匹配线如果结束? - Python Regex Match Line If Ends With? 正则表达式匹配确切的单词 - Regex expression to match exact word python正则表达式匹配确切的单词 - python Regex match exact word 正则表达式以一行中的CAPITAL词开始和结束,在CAPITAL单行词中的多行 - Regex starts and ends with CAPITAL word in a line, several lines amid CAPITAL single-line words 如何精确匹配字符串中的所有单词,包括以 ',?.?'"' 结尾的单词,但不使用正则表达式匹配任何其他标点符号? - How do I exact match all the words in a string includes those ends with '!,?.'"' but do not match those with any other punctuation using regex? 正则表达式匹配下一行中的单词 - Regex to match words in next line 如何将正确的单词与正则表达式python匹配? - How to match exact word with regex python? 如何捕捉以破折号结尾的单词及其后续单词? 正则表达式 - How to catch words that ends with dash and its following word? Regex Pandas 检查一行是否包含字符串而不是完全匹配 - Pandas check if a row contains a string rather than exact match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM