Question: I need to search a string for a list of several different matches, let's say I have this list:

```
['this', 'is', 'a', 'regex', 'test']

```

I want to see if any of those items is within a string, either using regex or any other method in Python.

I tried first just doing `string in list`, but that proved to be insufficient, so I tried concatenating the conditions in a regex like:

```
(this|is)(a|regex)(text)

```

But that tries to match several of the items as if they were concatenated.

Answer: 
You can use the built-in function `any()`:

```
In [1]: strs="I am a string"

In [2]: lis=['this', 'is', 'a', 'regex', 'test']

In [3]: any(x in strs for x in lis)
Out[3]: True

```

This will return `True` for something like `"thisisafoobar"` as well.

But if you want to match the exact word, then try `re.search()` or `str.split()`:

```
In [4]: import re

In [5]: any(re.search(r"\b{0}\b".format(x),strs) for x in lis)
Out[5]: True

In [6]: strs="foo bar"

In [7]: any(re.search(r"\b{0}\b".format(x),strs) for x in lis)
Out[7]: False

```

Using `str.split()`:

```
In [12]: strs="I am a string"

In [13]: spl=strs.split()    #use set(strs.split()) if the list returned is huge

In [14]: any(x in spl for x in lis)
Out[14]: True

In [15]: strs="Iamastring"

In [16]: spl=strs.split()

In [17]: any(x in spl for x in lis)
Out[17]: False

```
```
>>> l = ['this', 'is', 'a', 'regex', 'test']
>>> s = 'this is a test string'
>>> def check(elements, string):
...     for element in elements:
...         if element in string:
...             return True
...     return False
... 
>>> check(l, s)
True

```

Apparently this function has better performance than `any()`

```
import time

def main():
    # Making a huge list
    l = ['this', 'is', 'a', 'regex', 'test'] * 10000
    s = 'this is a test string'

    def check(elements, string):
        for element in elements:
            if element in string:
                return True
        return False

    def test_a(elements, string):
        """Testing check()"""
        start = time.time()
        check(elements, string)
        end = time.time()
        return end - start

    def test_b(elements, string):
        """Testing any()"""
        start = time.time()
        any(element in string for element in elements)
        end = time.time()
        return end - start

    print 'Using check(): %s' % test_a(l, s)
    print 'Using any(): %s' % test_b(l, s)

if __name__ == '__main__':
    main()

```

Results:

```
pearl:~ pato$ python test.py 
Using check(): 3.09944152832e-06
Using any(): 5.96046447754e-06
pearl:~ pato$ python test.py 
Using check(): 1.90734863281e-06
Using any(): 7.15255737305e-06
pearl:~ pato$ python test.py 
Using check(): 2.86102294922e-06
Using any(): 6.91413879395e-06

```

But if you combine `any()` with `map()` in something like `any(map(lambda element: element in string, elements))`, these are the results:

```
pearl:~ pato$ python test.py 
Using check(): 3.09944152832e-06
Using any(): 0.00903916358948
pearl:~ pato$ python test.py 
Using check(): 2.86102294922e-06
Using any(): 0.00799989700317
pearl:~ pato$ python test.py 
Using check(): 3.09944152832e-06
Using any(): 0.00829982757568

```