【问题标题】:Python - Regex (Re.Escape, Re.Findall); How To: Find sub-strings + a number of characters beyond the sub-strings within a string?Python - 正则表达式(Re.Escape,Re.Findall);如何:查找子字符串 + 字符串中子字符串之外的多个字符?
【发布时间】:2020-09-15 19:51:18
【问题描述】:

这可能是一个简单的问题。我正在学习如何使用正则表达式,但在对字符串执行特定任务时遇到了问题。

例如:

example_string = ";一,一;二,二;三,三;四,四"

desired_output = ["One, o", "Two, t", "Three, t", "Four, f"] #list输出OK

通过以下,我可以得到 ["One" , "Two" , "Three"]:

def findStringsInMiddle(a, b, text): 
    return re.findall(re.escape(a)+"(.*?)"+re.escape(b),text)

desired_output = findStringsInMiddle('; ' , ',' , example_string)

但我无法弄清楚如何正确配置它以获取我也想要的逗号+空格+any_type_of_character。

有什么建议吗?

谢谢!

【问题讨论】:

  • 谢谢大家的回答。每个人的解决方案实际上都有效,所以我不得不将我选择的解决方案建立在其他一些指标上。 Wiktor 的回答配置了我所拥有的实际功能,这是我特别要求的,他是第一个这样做的。不管怎样,下面所有的答案都很棒:很抱歉我不能全选!

标签: python regex string parsing findall


【解决方案1】:

您可以设置完整的模式(从分号到逗号后的第二个字母)并标记您要提取的组:

>>> s =  "; One, one; Two, two; Three, three; Four, four"
>>> re.findall(r"; (.*?,.{2})", s)
['One, o', 'Two, t', 'Three, t', 'Four, f']

【讨论】:

    【解决方案2】:

    这里有一个解决方案:

    example_string = "; One, one; Two, two; Three, three; Four, four"
    def findStringsInMiddle(text): 
        return re.findall("; (.+?, [a-z])",text)
    
    desired_output = findStringsInMiddle(example_string)
    desired_output
    

    输出:

    ['One, o', 'Two, t', 'Three, t', 'Four, f']
    

    【讨论】:

      【解决方案3】:

      您可以通过包含右侧分隔符并附加可选的(?:\s*.)? 组来稍微重新组织模式:

      def findStringsInMiddle(a, b, text): 
          return re.findall(re.escape(a)+"(.*?"+re.escape(b) + r"(?:\s*.)?)",text, flags=re.S)
      

      该模式看起来像;(.*?,(?:\s*.)?)(请参阅the regex demo)并将匹配:

      • ; - 左侧分隔符
      • (.*?,(?:\s*.)?) - 第 1 组:
        • .*? - 任何零个或多个字符,尽可能少
      • , - 逗号
      • (?:\s*.)? - 一个可选的非捕获组,匹配 1 次或 0 次出现的 0+ 个空格,然后是任何字符。

      请注意,我添加了 re.S 标志以使 . 也匹配换行符。

      full Python snippet below:

      import re
      example_string = "; One, one; Two, two; Three, three; Four, four"
      desired_output = ["One, o", "Two, t", "Three, t", "Four, f"] #list output is OK
      
      def findStringsInMiddle(a, b, text): 
          return re.findall(re.escape(a)+"(.*?"+re.escape(b) + r"(?:\s*.)?)",text, flags=re.S)
      
      desired_output = findStringsInMiddle('; ' , ',' , example_string)
      print(desired_output)
      # => ['One, o', 'Two, t', 'Three, t', 'Four, f']
      

      【讨论】:

        【解决方案4】:
        import re
        
        example_string = "; One, one; Two, two; Three, three; Four, four"
        
        pattern = re.compile(r";\s"  # The search string must start with a semoicolon and then a space character
                             r"([A-Z][a-z]+,\s.?)"  # Here is the capturing group, containing first a capital letter,
                             # some lowercase letters
                             # and finally a comma, space and zero or one characters
                             )
        print(re.findall(pattern,
                         example_string
                         )
              )
        
        

        输出:

        ['One, o', 'Two, t', 'Three, t', 'Four, f']
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-16
          • 2017-07-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多