简体   繁体   中英

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. 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

bind 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

How should I restrict this?

Kindly provide suggestion on the same.

If you want matches that end with 'abhishek' , you can try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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