This is the full string that I want to parse:
Response
--------
{
Return Code: 1
Key : <None>
Files : [
{
Return Code: 0
Data : 'Value' is 1
'Value' is two
This is third line of output
}
]
}
And this is how I want the parsed text to look like:
'Value' is 1
'Value' is two
This is third line of output
I've tried messing with re.findall()
but I cannot get exactly what I want.
This is a python script which trys to parse using regex ..
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
Output of script ..
[root@server ~]# python test.py
''uname'' is not recognized as an internal or external command,
operable program or batch file.
}
]
}
Option 1
If you want the three lines after Data:
, you can do something like this, capturing the three lines into Group 1:
match = re.search(r"Data\s*:\s*((?:[^\n]*[\r\n]+){3})", subject)
if match:
result = match.group(1)
Option 2
If you want all the lines after Data:
before the first line that has a }
, change the regex to :
Data\s*:\s*((?:[^\n]*(?:[\r\n]+(?!\s*}))?)+)
Using the following regex, you'll find the three strings you want.
Notice that this depends heavily on how the response is formatted.
>>> 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')]
You could also include the newline characters in the groups like this:
>>> re.findall(r"('Value'.*\n)(.*\n)(.*\n).*}",response)
[("'Value' is 1\n", "'Value' is two\n", 'This is third line of output\n')]
Depends on how you want to process this afterward.
UPDATE
How about this?
>>> re.findall(r"Data\s*:\s*(.*?)}",response,re.DOTALL)
["'Value' is 1\n'Value' is two\nThis is third line of output\n "]
This will find everything from the first 'Value' up untill the first '}'.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.