[英]How do I parse a sequentially organized multiline string into a data structure using regex/python?
[英]Using regex to parse multiline string
這是我要解析的完整字符串:
Response
--------
{
Return Code: 1
Key : <None>
Files : [
{
Return Code: 0
Data : 'Value' is 1
'Value' is two
This is third line of output
}
]
}
這就是我希望解析后的文本看起來像的樣子:
'Value' is 1
'Value' is two
This is third line of output
我已經嘗試過re.findall()
但是我無法得到我想要的。
這是一個python腳本,試圖使用正則表達式進行解析。
import subprocess,re
output = subprocess.check_output(['staf', 'server.com', 'PROCESS', 'START', 'SHELL', 'COMMAND', "'uname'", 'WAIT', 'RETURNSTDOUT', 'STDERRTOSTDOUT'])
result = re.findall(r'Data\s+:\s+(.*)', output, re.DOTALL)[0]
print result
腳本輸出
[root@server ~]# python test.py
''uname'' is not recognized as an internal or external command,
operable program or batch file.
}
]
}
選項1
如果要在Data:
之后添加三行,則可以執行以下操作,將三行捕獲到組1中:
match = re.search(r"Data\s*:\s*((?:[^\n]*[\r\n]+){3})", subject)
if match:
result = match.group(1)
選項2
如果要在Data:
之后的所有行,在包含}
的第一行之前,將正則表達式更改為:
Data\s*:\s*((?:[^\n]*(?:[\r\n]+(?!\s*}))?)+)
使用以下正則表達式,您將找到所需的三個字符串。
請注意,這在很大程度上取決於響應的格式。
>>> import re
>>> response = """
Response
--------
{
Return Code: 1
Key : <None>
Files : [
{
Return Code: 0
Data : 'Value' is 1
'Value' is two
This is third line of output
}
]
}"""
>>> re.findall(r"('Value'.*)\n(.*)\n(.*)\n.*}",response)
[("'Value' is 1", "'Value' is two", 'This is third line of output')]
您還可以在這樣的組中包括換行符:
>>> re.findall(r"('Value'.*\n)(.*\n)(.*\n).*}",response)
[("'Value' is 1\n", "'Value' is two\n", 'This is third line of output\n')]
取決於您以后如何處理。
更新
這個怎么樣?
>>> re.findall(r"Data\s*:\s*(.*?)}",response,re.DOTALL)
["'Value' is 1\n'Value' is two\nThis is third line of output\n "]
這將找到從第一個“值”到第一個“}”的所有內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.