【问题标题】:how to find a particular string in an element of array in python如何在python中的数组元素中查找特定字符串
【发布时间】:2016-08-27 06:16:25
【问题描述】:

我在 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”,因此它应该打印消息。

【问题讨论】:

    标签: python regex string list


    【解决方案1】:

    如果您想使用正则表达式执行此操作,则不能使用 in 运算符。请改用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"
    

    【讨论】:

      【解决方案2】:

      您不需要正则表达式,只需使用any

      >>> a = ["paul Parthipan","paul","sdds","sdsdd"]
      >>> last_name = "Parthipan".lower()
      >>> if any(last_name in name.lower() for name in a):
      ...     print("Matched")
      ... 
      Matched
      

      【讨论】:

        【解决方案3】:

        为什么不:

        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"
        

        但如果真的需要正则表达式部分,我无法想象如何在“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"
        

        编辑:

        嗯,用正则表达式你几乎没有可能......

        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'
        

        输出,只有第一个似乎没问题,所以只有 re.search 给出了正确的解决方案:

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

        【讨论】:

        • 在你的第一个代码 sn-p 中不需要import re,因为你不再使用它了...
        • 看不懂的部分需要正则表达式!
        • @Robert,你跑题了。阅读the documentation 了解(?...) 的作用。
        猜你喜欢
        • 2020-05-30
        • 2022-09-28
        • 1970-01-01
        • 2015-08-06
        • 1970-01-01
        • 2017-11-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多