0

Is there a way that I could search for 2 or more words in a string using regular expressions in Python.

for example, if I have a list of strings like this:

[red_house, red_door, red_seat, green_door, green_house, green_table]

... and I wanted to return red_house specifically, I could do:

red_house = [obj for obj in list if re.search("red", obj) and re.search("house", obj)] 

but is it possible to combine these 2 re.search's into 1?

Cheers,

iGwok
  • 323
  • 5
  • 18

3 Answers3

1
re.search("red.*house",my_String)

if you know it will be ordered that way ...

if you just know it will have the words, but it may be "house_red" or something instead of "red_house", this might work

 if set(re.findall("[a-zA-z]+",my_string)).intersection(["red","house"]) == set(["house","red"]) :
     #do something

since sets are unordered you are just looking for intersection with all the words

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
1

I would do it like this (if only two words in any order have to be matched):

l = "red_house red_door red_seat green_door green_house green_table house-red".split()
filter(lambda s: re.search('(red.*house)|(house.*red)', s), l)

Out[7]: ['red_house']

Ajasja
  • 809
  • 1
  • 16
  • 21
0

You could use look-aheads to get a more general AND operator (Regular Expressions: Is there an AND operator?).

But using two separate searches and the python and to combine them is clearer and simpler.

Community
  • 1
  • 1
sobel
  • 643
  • 5
  • 8