I'm trying to parse a string from log communicating with network which will be like
2019 Jun 30 15:40:17.561 NETWORK_MESSAGE
Direction = UE_TO_NETWORK
From: <1106994972>
To: <3626301680>
and here is my code:
import re
log = '2019 Jun 30 15:40:17.561 NETWORK_MESSAGE\r\nDirection = UE_TO_NETWORK\r\nFrom: <1106994972>\r\nTo: <3626301680>\r\n'
PATTERN = re.compile(
'(?P<time>\d{2}:\d{2}:\d{2}.\d{3}).*' # Time
'Direction = (?P<Direction>\S+).*' # Direction
'From: <(?P<From>\S+)>.*' # from
'To: <(?P<To>\S+)>', # to
re.DOTALL)
results = PATTERN.search(log)
print(results.group('From'))
However, I just found sometimes there will be reversed position between "From" and "To", just like the following.
2019 Jun 30 15:40:16.548 NETWORK_MESSAGE
Direction = NETWORK_TO_UE
To: <3626301680>
From: <1106994972>
Is it possible I can solve this with only one pattern?