Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的多条件正则表达式_Python_Regex - Fatal编程技术网

Python中的多条件正则表达式

Python中的多条件正则表达式,python,regex,Python,Regex,我需要在字符串中搜索几个不同匹配项的列表,假设我有以下列表: ['this', 'is', 'a', 'regex', 'test'] 我想使用正则表达式或Python中的任何其他方法查看这些项是否在字符串中 我首先尝试在列表中执行字符串,但结果证明这是不够的,因此我尝试在正则表达式中连接条件,如: (this|is)(a|regex)(text) 但这会尝试匹配多个项目,就好像它们是连接在一起的一样。您可以执行以下操作: if any(test in your_string for tes

我需要在字符串中搜索几个不同匹配项的列表,假设我有以下列表:

['this', 'is', 'a', 'regex', 'test']
我想使用正则表达式或Python中的任何其他方法查看这些项是否在字符串中

我首先尝试在列表中执行
字符串,但结果证明这是不够的,因此我尝试在正则表达式中连接条件,如:

(this|is)(a|regex)(text)
但这会尝试匹配多个项目,就好像它们是连接在一起的一样。

您可以执行以下操作:

if any(test in your_string for test in tests):
    ...

您可以使用内置函数
any()

对于类似于
“thisisafoobar”
的内容,这也将返回
True

但是,如果要匹配确切的单词,请尝试
re.search()
str.split()

使用str.split()

显然,此函数比
any()具有更好的性能

结果:

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
但是如果将
any()
map()
组合在类似
any(map(lambda元素:字符串中的元素,元素))
的内容中,则结果如下:

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

你到底想匹配什么?只是,看看一个字符串是否包含多个不同的模式/单词,而不必求助于多个条件。为什么要将它们分成多个组?正则表达式应该是
(这是一个|正则表达式|测试)
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
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()
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
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