Just use groupdict()
directly:
import re
def rtr_dict(txt):
pattern = '''
(?P<host>\d{1,}\.\d{1,}\.\d{1,}\.\d{1,}) # host name
\s+\S+\s+
(?P<user_name>(?<=-\s)(\w+|-)(?=\s))\s+\[ # user_name
(?P<time>([^[]+))\]\s+" # time
(?P<request>[^"]+)" # request
'''
if m:=re.match(pattern, txt, flags=re.VERBOSE):
return m.groupdict()
tgt='146.204.224.152 - feest6811 [21/Jun/2019:15:45:24 -0700] "POST /incentivize HTTP/1.1" 302 4622'
>>>rtr_dict(tgt)
{'host': '146.204.224.152', 'user_name': 'feest6811', 'time': '21/Jun/2019:15:45:24 -0700', 'request': 'POST /incentivize HTTP/1.1'}
Just could you please tell me how I could make it for more than just one line just the way I used a for loop for that.
Given:
tgt='''146.204.224.152 - feest6811 [21/Jun/2019:15:45:24 -0700] "POST /incentivize HTTP/1.1" 302 4622
146.204.224.153 - feest6811 [21/Jun/2019:15:45:24 -0700] "POST /incentivize HTTP/1.1" 302 4623
146.204.224.154 - feest6811 [21/Jun/2019:15:45:24 -0700] "POST /incentivize HTTP/1.1" 302 4624'''
If you have more than one match, you can return a list of dicts:
def rtr_dict(txt):
pattern = '''
(?P<host>\d{1,}\.\d{1,}\.\d{1,}\.\d{1,}) # host name
\s+\S+\s+
(?P<user_name>(?<=-\s)(\w+|-)(?=\s))\s+\[ # user_name
(?P<time>([^[]+))\]\s+" # time
(?P<request>[^"]+)" # request
'''
return [m.groupdict() for m in re.finditer(pattern, txt, flags=re.VERBOSE)]
>>> rtr_dict(tgt)
[{'host': '146.204.224.152', 'user_name': 'feest6811', 'time': '21/Jun/2019:15:45:24 -0700', 'request': 'POST /incentivize HTTP/1.1'}, {'host': '146.204.224.153', 'user_name': 'feest6811', 'time': '21/Jun/2019:15:45:24 -0700', 'request': 'POST /incentivize HTTP/1.1'}, {'host': '146.204.224.154', 'user_name': 'feest6811', 'time': '21/Jun/2019:15:45:24 -0700', 'request': 'POST /incentivize HTTP/1.1'}]
Or use a generator:
def rtr_dict(txt):
pattern = '''
(?P<host>\d{1,}\.\d{1,}\.\d{1,}\.\d{1,}) # host name
\s+\S+\s+
(?P<user_name>(?<=-\s)(\w+|-)(?=\s))\s+\[ # user_name
(?P<time>([^[]+))\]\s+" # time
(?P<request>[^"]+)" # request
'''
for m in re.finditer(pattern, txt, flags=re.VERBOSE):
yield m.groupdict()
>>> list(rtr_dict(tgt))
# same list of dicts...