user3914506
user3914506

Reputation: 65

Regex to print contents between dynamic word and a comma

I have a list and if I find a word in line matching list,then the dynamic word is passed to regex to print between that word and the first comma (,).

keyword=[car,bike,bus]

Text file:

I want a car Mercedes Benz,but its costly,and high maintenence.

Then output should be:

Mercedes Benz

I tried this code to pass the found keyword as a input:

keywords=['car','bike','bus'] 

with open('qwe.txt','r') as file:
    for line in file:            
        matches = []
        for word in line.split():
            if word in keywords:
                l=line
                matches.append(word)            
        if matches:
            a = ' '.join(matches)
            TEXT = a
            my_regex = r"\b(?=\w)" + re.escape(TEXT) + r"\b(?!\,)"   
            x = re.findall(my_regex, l)    

        else:
            print 'nil'

Upvotes: 0

Views: 93

Answers (2)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250991

Something like this:

>>> r = re.compile(r'\b(?:{})\b\s*(.*?),'.format('|'.join(map(re.escape, keywords))))
>>> r.findall(s)
['Mercedes Benz']
>>> s = "I want a car Mercedes Benz,but its costly, so I'll get car Volkswagen Beetle, as it is cheap."
>>> r.findall(s)
['Mercedes Benz', 'Volkswagen Beetle']

Upvotes: 4

Amal Murali
Amal Murali

Reputation: 76656

Change your regex like so:

my_regex = r"\b" +re.escape(TEXT) + r"\s*([ \w]+)(?=,)"  

Upvotes: 1

Related Questions