Python多行模式搜索

2024-04-28 08:37:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的文本,我需要对其进行解析,以提取三个值的所有组。对于这个特定的示例,我需要这样的输出:[1,1,1],[2,2,2],[3,2,3],[4,2,4] 我试图使用此注册表表达式:

re.findall(r'measId \d+,[\n\r]measObjectId \d+[\n\r],reportConfigId \d+',output)

但它总是返回零结果。我已经尝试了多种组合,有re.MULTILINE标志,但没有一个,但没有区别。 我做错了什么?有什么建议吗

measIdToAddModList {
          {
            measId 1,
            measObjectId 1,
            reportConfigId 1
          },
          {
            measId 2,
            measObjectId 2,
            reportConfigId 2
          },
          {
            measId 3,
            measObjectId 2,
            reportConfigId 3
          },
          {
            measId 4,
            measObjectId 2,
            reportConfigId 4
          }

Tags: 文本re示例output表达式注册表标志建议
2条回答

模式[\w]+\s[\d]将一行与您需要的匹配

使用python获取所需的一切。假设您的输入是一个名为inputstr

import re
from collections import defaultdict

output = defaultdict(list)

pattern = re.compile(r'(?P<key>[\w]+)\s(?P<value>[\d])')
for line in input.splitlines():
  match = pattern.search(line)
  if match:
    key = match.group('key')
    value = match.group('value')
    output[key].append(value)

output则是一个字典,其中键是文本值,值是文本右侧带有数字的列表

{'measId': ['1', '2', '3', '4'],
 'measObjectId': ['1', '2', '2', '2'],
 'reportConfigId': ['1', '2', '3', '4']}

不确定您需要的输出,但完全可以从那里建模。例如:

>>> list(zip(*output.values()))
[('1', '1', '1'), ('2', '2', '2'), ('3', '2', '3'), ('4', '2', '4')]

Google Colab中查看它

这是最简单的解决方案。仅当正好存在三个字段时,此选项才有效:

re.findall(r'\{\s+(\w+\s+\d+),\s+(\w+\s+\d+),\s+(\w+\s+\d+)\s+}', s)
#[('measId 1', 'measObjectId 1', 'reportConfigId 1'), 
# ('measId 2', 'measObjectId 2', 'reportConfigId 2'), 
# ('measId 3', 'measObjectId 2', 'reportConfigId 3'), 
# ('measId 4', 'measObjectId 2', 'reportConfigId 4')]

说明:

\{          # Opening curly brace 
\s+         # One or more spaces
(\w+\s+\d+) # word, spaces, digits
,\s+        # comma, spaces
(\w+\s+\d+)
,\s+
(\w+\s+\d+)
\s+         # spaces
}           # Closing curly brace

相关问题 更多 >