[英]Python multiline pattern search
我有以下文本,我需要對其進行解析以提取所有三個值的組。 對於這個具體的例子,我需要一個 output 像這樣:[1,1,1],[2,2,2],[3,2,3],[4,2,4] 我試圖使用這個 reg expr :
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
}
這是最天真的解決方案。 它僅在恰好存在三個字段時才有效:
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
模式[\w]+\s[\d]
匹配您需要的行。
使用 python 獲得您需要的一切。 假設您將輸入作為名為input
的str
。
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']}
不確定您需要的 output,但絕對可以從那里獲得 model。 例如:
>>> list(zip(*output.values()))
[('1', '1', '1'), ('2', '2', '2'), ('3', '2', '3'), ('4', '2', '4')]
在Google Colab中查看
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.