Regular expression to extract a message

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

Answers


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.')

Need Your Help

Efficient way to look up value based on a key in php

php dictionary lookup-tables key-value

With a list of around 100,000 key/value pairs (both string, mostly around 5-20 characters each) I am looking for a way to efficiently find the value for a given key.

C# Load configuration from XML file

c# xml-serialization

I have a console application which will be triggered by a batch file. The application must have to load the configuration from an XML file for running... Suppose if the batch file sends a parameter

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.