Reputation: 167
i have a script that read lines of files.. and some of the lines contain Error messages.. so i have made a loop ( here it's just for one line ) to find those lines and extract the messages:
import re
data = "15:31:17 TPP E Line 'MESSAGE': There is a technical problem in the server."
if (re.findall(".*E Line.*",data)):
err = re.match(r'\'MESSAGE\':\s(*$)',data)
print err
I have and error when i execute this script :/ i'd like it to return:
There is a technical problem in the server
Upvotes: 2
Views: 100
Reputation: 43447
You don't need a regular expression for this if they all follow the same format:
>>> data = "15:31:17 TPP E Line 'MESSAGE': There is a technical problem in the server."
>>> data.rsplit(':', 1)[1]
' There is a technical problem in the server.'
But if you must use them...
>>> data = "15:31:17 TPP E Line 'MESSAGE': There is a technical problem in the server."
>>> ms = re.search(r"'MESSAGE': (.*)$", data)
>>> ms.group(1)
'There is a technical problem in the server.'
If you wanted you could extract other information as well:
>>> ms = re.match(r"(\d\d:\d\d:\d\d)\s+(\S+)\s+(\S+)\s+Line\s+'MESSAGE':\s+(.*)", data)
>>> ms.groups()
('15:31:17', 'TPP', 'E', 'There is a technical problem in the server.')
Upvotes: 4
Reputation:
Try this:
import re
data = "15:31:17 TPP E Line 'MESSAGE': There is a technical problem in the server."
r = re.compile("^.*E Line.*'MESSAGE':[ ]*([^ ].*)$")
m = r.match(data)
if m:
err = m.group(1)
print(err)
Of course you should compile the regex outside of the loop.
Upvotes: 1