【问题标题】:Python Regex to find String between two stringsPython正则表达式在两个字符串之间查找字符串
【发布时间】:2019-07-10 18:41:58
【问题描述】:

我正在尝试使用正则表达式来查看字符串的特定部分并获取介于两者之间的内容,但我无法为此获得正确的正则表达式模式。

我最大的问题是试图为此形成一个正则表达式模式。我已经尝试了一系列接近所列示例的变体。它应该很接近。

import re

toFind = ['[]', '[x]']
text = "| Completed?|\n|------|:---------:|\n|Link Created    |   []   |\n|Research Done   |   [X] "

# Regex to search between parameters and make result lowercase if there are any uppercase Chars
result = (re.search("(?<=Link Created)(.+?)(?=Research Done)", text).lower())

# Gets rid of whitespace in case they move the []/[x] around
result = result.replace(" ", "")

if any(x in result for x in toFind):
    print("Exists")
else:
    print("Doesn't Exist")

快乐之路: 我采用字符串(文本)并使用正则表达式来获取 Link Created 和 Research Done 之间的子字符串。

然后将结果设为小写并去掉空格,以防他们移动 []/[x]。然后它查看 '[]' 或 '[x]' 的字符串(结果)并打印。

实际输出: 目前我得到的只是无,因为正则表达式语法已关闭......

【问题讨论】:

    标签: python regex python-3.x


    【解决方案1】:

    如果你想让. 匹配换行符,你可以使用re.S 选项。

    此外,在继续进一步调用之前检查正则表达式是否匹配似乎是一个更好的主意。您对lower() 的调用给了我一个错误,因为正则表达式不匹配,因此仅在result 评估为true 时调用result.group(0).lower() 更安全。

    import re
    
    toFind = ['[]', '[x]']
    text = "| Completed?|\n|------|:---------:|\n|Link Created    |   []   |\n|Research Done   |   [X] "
    
    # Regex to search between parameters and make result lowercase if there are any uppercase Chars
    result = (re.search("(?<=Link Created)(.+?)(?=Research Done)", text, re.S))
    
    if result:
        # Gets rid of whitespace in case they move the []/[x] around
        result = result.group(0).lower().replace(" ", "")
    
        if any(x in result for x in toFind):
            print("Exists")
        else:
            print("Doesn't Exist")
    else:
        print("re did not match")
    

    PS:所有re 选项都记录在re module documentation 中。搜索 re.DOTALL 以获取有关 re.S 的详细信息(它们是同义词)。如果要组合选项,请使用按位或。例如,re.S|re.I 将有 . 匹配换行符并进行不区分大小写的匹配。

    【讨论】:

    • 我知道我已经很接近了~ 正则表达式每次都会杀死我。我一定会看看文档。谢谢。
    • 没问题,我对这个网站的手续还是新手。
    • 这很正常。我感谢信任投票。我很快就会删除这条评论和上面的评论,因为它们很健谈,并没有真正为这个问答对的长期价值做出贡献。随着时间的推移,我学到了另一个好的网站实践:删除没有持久相关性的 cmets。
    【解决方案2】:

    我相信是\n 换行符引起了问题。你可以使用[\s\S]+ 来解决这个问题:

    import re
    
    toFind = ['[]', '[x]']
    text = "| Completed?|\n|------|:---------:|\n|Link Created    |   []   |\n|Research Done   |   [X] "
    
    # New regex to match text between
    # Remove all newlines, tabs, whitespace and column separators
    result = re.search(r"Link Created([\s\S]+)Research Done", text).group(1)
    result = re.sub(r"[\n\t\s\|]*", "", result)
    
    if any(x in result for x in toFind):
        print("Exists")
    else:
        print("Doesn't Exist")
    

    【讨论】:

      【解决方案3】:

      似乎正则表达式对于这个特定的工作来说是多余的,除非我遗漏了一些东西(我也不清楚为什么你需要从子字符串中删除空格的步骤)。您可以在“Link Created”上拆分,然后在“Research Done”上拆分以下字符串。

      text = "| Completed?|\n|------|:---------:|\n|Link Created    |   []   |\n|Research Done   |   [X] "
      
      s = text.split("Link Created")[1].split("Research Done")[0].lower()
      
      if "[]" in s or "[x]" in s:
          print("Exists")
      else:
          print("Doesn't Exist")
      
      # Exists
      

      【讨论】:

      • 我猜我可以看到正则表达式有点矫枉过正。这实际上是一个更大程序的一部分,我想我会删除空格,因为当人们编辑一些将成为“文本”字符串的代码时,他们可能会不小心将空格添加到 [] 或 [x ].
      猜你喜欢
      • 2019-05-19
      • 2012-05-19
      • 1970-01-01
      • 1970-01-01
      • 2012-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多