【问题标题】:Python regex findall returning empty list after parsing a text filePython regex findall 在解析文本文件后返回空列表
【发布时间】:2021-08-05 12:33:57
【问题描述】:

我正在尝试使用 Python 的 re 模块在 .txt 文件中解析来自应用程序的一些对话,但尽管在文件样本上使用 regex101 时,它在我打开时无法正常工作文件并实际尝试解析它。

txt 文件的结构是dd/mm/yyyy hh:mm - Message Author: message text\n,我试图只获取Name: message \n 部分。我正在使用以下模式(?<=\d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}\s\-\s)(.*:.*$)。我的代码看起来或多或少类似于以下内容:

buffer = open(file, 'r', encoding = 'UTF-8').read()
pattern = re.compile(r'(?<=\d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}\s\-\s)(.*:\s)(.*$)')
matches = re.findall(pattern, buffer)

正如标题所说,findall 返回并为空列表,我不知道为什么。以下示例在 regex101 上按预期工作:

20/04/2021 09:54 - Person 1: this is an example text. Will it match?
20/04/2021 09:54 - Person 2: I think it does.

【问题讨论】:

  • 此代码按预期工作。您的输入文件一定不正确。
  • 谢谢,约翰。我去看看有没有问题。

标签: python regex re findall


【解决方案1】:

环视是“昂贵的”。更好地匹配您想要的内容并捕捉有趣的部分。
也就是说,您可能会使用更简单的表达式:

^\d+[^-]+-\s+(?P<person>[^:]+):\s+(?P<text>.+)

a demo on regex101.com

【讨论】:

    【解决方案2】:

    亲吻:删除$。它匹配字符串的结尾。您需要匹配行尾,re.M 在这里可能会有所帮助。但是删除$ 更简单。

    (?<=\d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}\s\-\s)(.*:\s)(.*)
    

    但即使是“亲吻”er:您不需要向后看或通过斜杠转义,因为如果您在表达式中使用捕获组,re.findall 会返回捕获的字符串

    使用

    pattern = re.compile(r'\b\d{2}/\d{2}/\d{4}\s*\d{2}:\d{2}\s*-\s*(?P<name>.*):\s*(?P<message>.*)')
    with open(file, 'r', encoding = 'UTF-8') as buffer:
        matches = [match.groupdict() for match in pattern.finditer(test_str)]
    

    Regex proof | Python code

    解释

    --------------------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    --------------------------------------------------------------------------------
      \d{2}                    digits (0-9) (2 times)
    --------------------------------------------------------------------------------
      /                        '/'
    --------------------------------------------------------------------------------
      \d{2}                    digits (0-9) (2 times)
    --------------------------------------------------------------------------------
      /                        '/'
    --------------------------------------------------------------------------------
      \d{4}                    digits (0-9) (4 times)
    --------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                               more times (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      \d{2}                    digits (0-9) (2 times)
    --------------------------------------------------------------------------------
      :                        ':'
    --------------------------------------------------------------------------------
      \d{2}                    digits (0-9) (2 times)
    --------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                               more times (matching the most amount
                               possible))
    --------------------------------------------------------------------------------
      -                        '-'
    --------------------------------------------------------------------------------
      \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                               more times (matching the most amount
                               possible))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多