I'm trying to write a pattern to make a dictionary like :
string= '30.95.91.251 - larson8319 [21/Jun/2019:16:02:02 -0700] "PUT /one-to-one/whiteboard HTTP/1.0" 401 7270'
to look like:
dic= {"host":"30.95.91.251",
"user_name":"larson8319",
"time":"21/Jun/2019:16:02:02 -0700",
"request":"PUT /one-to-one/whiteboard HTTP/1.0"}
using this code:
pattern = '''
(?P<host>.*)
(-\ )
(?P<user_name>\w*)
(?P<time>\W.+)
(?P<request>\w+)
'''
for item in re.finditer(pattern, logdata, re.VERBOSE):
print(item.groupdict())'
but I couldn’t make the brackets disappear and order the request part.
Be more specific and use character classes (
[...]
):See a demo on regex101.com.
Or - use a parser altogether:
Which would yield