Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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_String_List - Fatal编程技术网

如何在python中的数组元素中查找特定字符串

如何在python中的数组元素中查找特定字符串,python,regex,string,list,Python,Regex,String,List,我有一个python字符串列表,如果列表中的一个元素包含单词“parthipan”,我应该打印一条消息。但是下面的脚本不起作用 import re a = ["paul Parthipan","paul","sdds","sdsdd"] last_name = "Parthipan" my_regex = r"(?mis){0}".format(re.escape(last_name)) if my_regex in a: print "matched" 列表的第一个元素包含单词“pa

我有一个python字符串列表,如果列表中的一个元素包含单词“parthipan”,我应该打印一条消息。但是下面的脚本不起作用

import re
a = ["paul Parthipan","paul","sdds","sdsdd"]
last_name = "Parthipan"
my_regex = r"(?mis){0}".format(re.escape(last_name))
if my_regex in a:
    print "matched"

列表的第一个元素包含单词“parthipan”,因此它应该打印消息。

如果要使用regexp执行此操作,则不能在操作符中使用
。改用
re.search()
。但它使用字符串,而不是整个列表

for elt in a:
    if re.search(my_regexp, elt):
        print "Matched"
        break # stop looking
或者以更实用的方式:

if any(re.search(my_regexp, elt) for elt in a)):
    print "Matched"

如果要使用regexp执行此操作,则不能在
操作符中使用
。改用
re.search()
。但它使用字符串,而不是整个列表

for elt in a:
    if re.search(my_regexp, elt):
        print "Matched"
        break # stop looking
或者以更实用的方式:

if any(re.search(my_regexp, elt) for elt in a)):
    print "Matched"

您不需要正则表达式就可以简单地使用它


您不需要正则表达式就可以简单地使用它

为什么不:

a = ["paul Parthipan","paul","sdds","sdsdd"]
last_name = "Parthipan"
if any(last_name in ai for ai in a):
    print "matched"
这一部分的目的是什么:

...
import re
my_regex = r"(?mis){0}".format(re.escape(last_name))
...
编辑:

我只是太瞎了,看不清你为什么需要正则表达式。如果你能给出一些真实的输入和输出,那将是最好的。这是一个小例子,也可以这样做:

a = ["paul Parthipan","paul","sdds","sdsdd",'Mala_Koala','Czarna,Pala']
last_name = "Parthipan"
names=[]
breakers=[' ','_',',']
for ai in a:
    for b in breakers:
        if b in ai:
            names.append(ai.split(b))
full_names=[ai for ai in names if len(ai)==2]
last_names=[ai[1] for ai in full_names]

if any(last_name in ai for ai in last_names):
    print "matched"
但如果真的需要regex部分,我无法想象如何在“Parthipan”中找到“(?mis)Parthipan”。最简单的是在“(?mis)Parthipan”中的反向“Parthipan”。就像这里

import re
a = ["paul Parthipan","paul","sdds","sdsdd",'Mala_Koala','Czarna,Pala']
last_name = "Parthipan"
names=[]
breakers=[' ','_',',']
for ai in a:
    for b in breakers:
        if b in ai:
            names.append(ai.split(b))
full_names=[ai for ai in names if len(ai)==2]
last_names=[r"(?mis){0}".format(re.escape(ai[1])) for ai in full_names]
print last_names
if any(last_name in ai for ai in last_names):
    print "matched"
编辑:

嗯,有了regex你几乎没有什么可能

import re
a = ["paul Parthipan","paul","sdds","sdsdd",'jony-Parthipan','koala_Parthipan','Parthipan']
lastName = "Parthipan"
myRegex = r"(?mis){0}".format(re.escape(lastName))

strA=';'.join(a)
se = re.search(myRegex, strA)
ma = re.match(myRegex, strA)
fa = re.findall(myRegex, strA)
fi=[i.group() for i in re.finditer(myRegex, strA, flags=0)]
se = '' if se is None else se.group()
ma = '' if ma is None else ma.group()

print se, 'match' if any(se) else 'no match'
print ma, 'match' if any(ma) else 'no match'
print fa, 'match' if any(fa) else 'no match'
print fi, 'match' if any(fi) else 'no match'
输出,只有第一个看起来正常,因此只有重新搜索才能给出正确的解决方案:

Parthipan match
 no match
['Parthipan', 'Parthipan', 'Parthipan', 'Parthipan'] match
['Parthipan', 'Parthipan', 'Parthipan', 'Parthipan'] match
为什么不:

a = ["paul Parthipan","paul","sdds","sdsdd"]
last_name = "Parthipan"
if any(last_name in ai for ai in a):
    print "matched"
这一部分的目的是什么:

...
import re
my_regex = r"(?mis){0}".format(re.escape(last_name))
...
编辑:

我只是太瞎了,看不清你为什么需要正则表达式。如果你能给出一些真实的输入和输出,那将是最好的。这是一个小例子,也可以这样做:

a = ["paul Parthipan","paul","sdds","sdsdd",'Mala_Koala','Czarna,Pala']
last_name = "Parthipan"
names=[]
breakers=[' ','_',',']
for ai in a:
    for b in breakers:
        if b in ai:
            names.append(ai.split(b))
full_names=[ai for ai in names if len(ai)==2]
last_names=[ai[1] for ai in full_names]

if any(last_name in ai for ai in last_names):
    print "matched"
但如果真的需要regex部分,我无法想象如何在“Parthipan”中找到“(?mis)Parthipan”。最简单的是在“(?mis)Parthipan”中的反向“Parthipan”。就像这里

import re
a = ["paul Parthipan","paul","sdds","sdsdd",'Mala_Koala','Czarna,Pala']
last_name = "Parthipan"
names=[]
breakers=[' ','_',',']
for ai in a:
    for b in breakers:
        if b in ai:
            names.append(ai.split(b))
full_names=[ai for ai in names if len(ai)==2]
last_names=[r"(?mis){0}".format(re.escape(ai[1])) for ai in full_names]
print last_names
if any(last_name in ai for ai in last_names):
    print "matched"
编辑:

嗯,有了regex你几乎没有什么可能

import re
a = ["paul Parthipan","paul","sdds","sdsdd",'jony-Parthipan','koala_Parthipan','Parthipan']
lastName = "Parthipan"
myRegex = r"(?mis){0}".format(re.escape(lastName))

strA=';'.join(a)
se = re.search(myRegex, strA)
ma = re.match(myRegex, strA)
fa = re.findall(myRegex, strA)
fi=[i.group() for i in re.finditer(myRegex, strA, flags=0)]
se = '' if se is None else se.group()
ma = '' if ma is None else ma.group()

print se, 'match' if any(se) else 'no match'
print ma, 'match' if any(ma) else 'no match'
print fa, 'match' if any(fa) else 'no match'
print fi, 'match' if any(fi) else 'no match'
输出,只有第一个看起来正常,因此只有重新搜索才能给出正确的解决方案:

Parthipan match
 no match
['Parthipan', 'Parthipan', 'Parthipan', 'Parthipan'] match
['Parthipan', 'Parthipan', 'Parthipan', 'Parthipan'] match

无需在您的第一个代码段中重新导入
,因为您不再使用它了…您不理解的部分需要regexp@罗伯特,你出轨了。阅读以了解
(?…)
的作用。无需在第一个代码段中导入re,因为您不再使用它了…您不理解的部分需要regexp@罗伯特,你出轨了。阅读以了解
(?…)
的作用。