正则表达式在动态单词和逗号之间打印内容

时间:2014-08-10 14:22:40

标签: python regex string

我有一个列表,如果我在行匹配列表中找到一个单词,那么动态单词将传递给正则表达式以在该单词和第一个逗号(,)之间打印。

keyword=[car,bike,bus]

文字档案:

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

然后输出应该是:

Mercedes Benz

我尝试使用此代码将找到的关键字作为输入传递:

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'

2 个答案:

答案 0 :(得分:4)

这样的事情:

>>> 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']

答案 1 :(得分:1)

像这样改变你的正则表达式:

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