我在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”,因此它应该打印消息。你知道吗
Tags:
您不需要正则表达式,只需使用^{} 。你知道吗
如果要使用regexp执行此操作,则不能使用
in
运算符。改用re.search()
。但它适用于字符串,不是一个完整的列表。你知道吗或者更实用的风格:
为什么不:
这一部分的目的是什么:
编辑:
我只是太瞎了,看不清你需要正则表达式干什么。如果你能给出一些真实的输入和输出,那就最好了。这是一个小例子,也可以这样做:
但如果真的需要regex部分,我无法想象如何找到'(?“Parthipan”中的“Parthipan”。最简单的方法是反向“Parthipan”in'(?(mis)Parthipan'。就像这里。。。你知道吗
编辑:
嗯,有了正则表达式,你几乎没有可能。。。你知道吗
输出,只有第一个看起来没问题,所以只有检索给出适当的解决方案:
相关问题 更多 >
编程相关推荐