2015-10-29 18 views
8

可以說我有一個字符串列表,如何匹配python中正則表達式中的字符串列表中的任何字符串?

string_lst = ['fun', 'dum', 'sun', 'gum'] 

我想打一個正則表達式,在此,在一個點,我可以匹配任何字符串我在該列表中,一個組內,例如:

import re 
template = re.compile(r".*(elem for elem in string_lst).*") 
template.match("I love to have fun.") 

什麼是正確的方法來做到這一點?或者人們必須製作多個正則表達式並將它們全部單獨匹配到字符串?

+0

加入與'數組元素|'膠,將形成的字符串作爲'樂趣|達姆|太陽| gum'這可以在正則表達式中使用。 – Tushar

+3

're.search('|'.join(string_lst),input_string)' –

+0

'any(z in string_list for z in re.findall(r「['\ w] +」,'這只是爲了好玩') )' –

回答

13
string_lst = ['fun', 'dum', 'sun', 'gum'] 
x="I love to have fun." 

print re.findall(r"(?=("+'|'.join(string_lst)+r"))",x) 

不能使用match,因爲它會從start.Use findall,而不是匹配。

輸出:['fun']

使用search將只得到第一個match.So使用findall代替。

如果您有重疊的匹配不是在同一點開始,也可以使用lookahead

+0

爲什麼不're.search'? –

+0

這樣一個優雅的解決方案! –

+1

確實非常好的解決方案。謝謝 – user3341078

1

除了正則表達式,你可以使用列表理解,希望它不脫離主題。

import re 
def match(input_string, string_list): 
    words = re.findall(r'\w+', input_string) 
    return [word for word in words if word in string_list] 

>>> string_lst = ['fun', 'dum', 'sun', 'gum'] 
>>> match("I love to have fun.", string_lst) 
['fun'] 
0

你應該確保逃脫字符串組合成一個正則表達式之前正確

>>> import re 
>>> string_lst = ['fun', 'dum', 'sun', 'gum'] 
>>> x = "I love to have fun." 
>>> regex = re.compile("(?=(" + "|".join(map(re.escape, string_lst)) + "))") 
>>> re.findall(regex, x) 
['fun'] 
5

regex module已經命名列表(實際上套):

#!/usr/bin/env python 
import regex as re # $ pip install regex 

p = re.compile(r"\L<words>", words=['fun', 'dum', 'sun', 'gum']) 
if p.search("I love to have fun."): 
    print('matched') 

這裏words只是一個名字,你可以使用你喜歡的任何東西來代替。
.search()使用方法而不是指定列表之前/之後的.*

要使用STDLIB的re模塊模擬命名名單:

#!/usr/bin/env python 
import re 

words = ['fun', 'dum', 'sun', 'gum'] 
longest_first = sorted(words, key=len, reverse=True) 
p = re.compile(r'(?:{})'.format('|'.join(map(re.escape, longest_first)))) 
if p.search("I love to have fun."): 
    print('matched') 

re.escape()被用來逃跑的正則表達式元字符,如.*?個別字內(從字面上匹配的話)。
sorted()模擬regex行爲,並首先將其最長的單詞的備選方案中,比較:

>>> import re 
>>> re.findall("(funny|fun)", "it is funny") 
['funny'] 
>>> re.findall("(fun|funny)", "it is funny") 
['fun'] 
>>> import regex 
>>> regex.findall(r"\L<words>", "it is funny", words=['fun', 'funny']) 
['funny'] 
>>> regex.findall(r"\L<words>", "it is funny", words=['funny', 'fun']) 
['funny'] 
相關問題