【问题标题】:How to return full substring from partial substring match in python as a list?如何从python中的部分子字符串匹配返回完整字符串作为列表?
【发布时间】:2021-10-29 07:23:58
【问题描述】:

我有不同长度的字符串,必须检查匹配“tion”、“ex”、“ph”、“ost”、“ast”、“ist”模式的子字符串,忽略大小写和位置,即前缀/后缀/单词中间。必须在新列表中返回匹配的单词,而不是仅在匹配的子字符串元素中返回。使用下面的代码,我可以返回一个没有完整匹配词的匹配子字符串元素的新列表。

def latin_ish_words(text):
    import re
    pattern=re.compile(r"tion|ex|ph|ost|ast|ist")
    matches=pattern.findall(text)
    return matches
latin_ish_words("This functions as expected")

结果如下:['tion', 'ex']

我想知道如何将整个单词而不是匹配的子字符串元素返回到新列表中?

【问题讨论】:

    标签: python regex string substring findall


    【解决方案1】:

    对于与空白边界不区分大小写的匹配,您可以使用:

    (?i)(?<!\S)\w*(?:tion|ex|ph|[oia]st)\w*(?!\S)
    

    模式匹配:

    • (?i) 用于不区分大小写匹配的内联修饰符(或使用 re.I
    • (?&lt;!\S) 向左声明空白边界
    • \w*匹配可选单词字符
    • (?:非捕获组
      • tion|ex|ph|[oia]st 匹配 tion ex phpost ist ast 使用字符类
    • )关闭非捕获组
    • \w*匹配可选单词字符
    • (?!\S) 断言右边的空白边界

    Regex demo | Python demo

    def latin_ish_words(text):
        import re
        pattern = r"(?i)(?<!\S)\w*(?:tion|ex|ph|[oia]st)\w*(?!\S)"
        return re.findall(pattern, text)
    
    print(latin_ish_words("This functions as expected"))
    

    输出

    ['functions', 'expected']
    

    【讨论】:

      【解决方案2】:

      无视情况

      pattern=re.compile(r"tion|ex|ph|ost|ast|ist")
      matches=pattern.findall(text)
      

      不这样做,请考虑以下示例

      import re
      pattern=re.compile(r"tion|ex|ph|ost|ast|ist")
      text = "SCREAMING TEXT"
      print(pattern.findall(text))
      

      输出

      []
      

      尽管应该有EX,你应该像这样添加re.IGNORECASE标志

      import re
      pattern=re.compile(r"tion|ex|ph|ost|ast|ist", re.IGNORECASE)
      text = "SCREAMING TEXT"
      print(pattern.findall(text))
      

      输出

      ['EX']
      

      【讨论】:

        【解决方案3】:

        你可以使用

        pattern=re.compile(r"\w*?(?:tion|ex|ph|ost|ast|ist)\w*")
        pattern=re.compile(r"[a-zA-Z]*?(?:tion|ex|ph|ost|ast|ist)[a-zA-Z]*")
        pattern=re.compile(r"[^\W\d_]*?(?:tion|ex|ph|ost|ast|ist)[^\W\d_]*")
        

        正则表达式(参见the regex demo)匹配

        • \w*? - 零个或多个但尽可能少的字字符
        • (?:tion|ex|ph|ost|ast|ist) - 字符串之一
        • \w* - 零个或多个但尽可能多的字字符

        [a-zA-Z] 部分将仅匹配 ASCII 字母,[^\W\d_] 将匹配任何 Unicode 字母。

        注意re.findall 使用非捕获组,否则捕获的子字符串也会进入输出列表。

        如果只需要匹配字母单词,而需要将它们作为整个单词进行匹配,请添加word boundariesr"\b[a-zA-Z]*?(?:tion|ex|ph|ost|ast|ist)[a-zA-Z]*\b"

        Python demo

        import re
        def latin_ish_words(text):
            import re
            pattern=re.compile(r"\w*?(?:tion|ex|ph|ost|ast|ist)\w*")
            return pattern.findall(text)
         
        print(latin_ish_words("This functions as expected"))
        # => ['functions', 'expected']
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-05-26
          • 1970-01-01
          • 2013-03-07
          • 2021-07-15
          • 1970-01-01
          • 2011-06-17
          • 2023-03-28
          • 1970-01-01
          相关资源
          最近更新 更多