Reputation:
i have this variable:
Application name: Clarion.Pricing.Grid.Service^
Source: EC2AMAZ-ITEJKDI
Timestamp: 2019-01-21T03:52:01.798Z
Message: Connection id ""0HLJV4AI9OCV6"", Request id ""0HLJV4AI9OCV6:000000=
08"": An unhandled exception was thrown by the application.
and i want to get strings after Application name and Source, i'm not good with regex so i created 2 separate expressions:
regex1=r'Application name:\s*(.+?)\s+Source'
regex2=r'Source:\s*(.+?)\s+Timestamp:'
a = re.findall(regex1 ,email_body)
b = re.findall(regex2 ,email_body)
how to combine these 2 into one, and i need separate regex for returning string after Message
desired output
Clarion.Pricing.Grid.Service EC2AMAZ-ITEJKDI Connection id ""0HLJV4AI9OCV6"", Request id ""0HLJV4AI9OCV6:000000=
08"": An unhandled exception was thrown by the application.
Upvotes: 0
Views: 48
Reputation: 6151
You can use this regex:
(?:Application name:\s*(.+?)\s+(?=Source))|(?:Source:\s*(.+?)\s+(?=Timestamp:))
Explanation: you need to use positive lookahead (?=
so that it doesn't consume the "Source" characters, or else it can't be detected by the second alternative, same for "Timestamp" by design even if it doesn't really matter here. The (?:
are used to form groups of regexp that doesn't capture.
To add message, i assume you want to capture until the end of your input:
(?:Application name:\s*(.+?)\s+(?=Source))|(?:Source:\s*(.+?)\s+(?=Timestamp:))|(?:Message:\s*([\s\S]*)$)
Upvotes: 1