0
votes

I have:

data = [[u'text'],[u'element'],[u'text00']]
pattern = text
pattern2 = EL
pattern3 = 00 

Using regex I want to search and return:

text, text00 # for pattern
element      # for pattern2
text00       # for pattern3
2
What are you expecting? - TerryA
To find the pattern. If user enters "red" and the list is ["red", "lightred", "orange red", "blue"] then it should return true. - user1552586
That's what if pattern in data: already returns - TerryA
@Haidro I think OP wants "red", "lightred", "orange red" to be returned for red. - Santosh Kumar
@SantoshKumar hm, thought so. Just odd how the example structure already has red in it - TerryA

2 Answers

2
votes
import re
data = [[u'text'], [u'element'], [u'text00']]
patterns = [u'text', u'EL', u'00']
results = []
for pattern in patterns:
    results.append([x[0] for x in data if re.search(pattern, x[0], flags=re.I)])
print  results

or, more concisely:

import re
data = [[u'text'], [u'element'], [u'text00']]
patterns = [u'text', u'EL', u'00']
results = [[x[0] for x in data if re.search(pattern, x[0], flags=re.I)] for pattern in patterns]
print  results
2
votes

I think what you're looking for is any():

>>> L = ["red", "lightred", "orange red", "blue"]
>>> keyword = 'red'
>>> import re
>>> any(re.search(keyword, i) is not None for i in L)
True