fork download
  1. import re
  2. logdata = r"""159.253.153.40 - - [21/Jun/2019:15:46:10 -0700] "POST /e-business HTTP/1.0" 504 19845
  3. 136.195.158.6 - feeney9464 [21/Jun/2019:15:46:11 -0700] "HEAD /open-source/markets HTTP/2.0" 204 21149"""
  4. pattern = r'''
  5. (?P<host>\d+(?:\.\d+){3}) # 1+ digits and 3 occurrenses of . and 3 digits
  6. \s+\S+\s+ # 1+ whitespaces, 1+ non-whitespaces, 1+ whitespaces
  7. (?P<user_name>\S+)\s+\[ # 1+ non-whitespaces (Group "user_name"), 1+ whitespaces and [
  8. (?P<time>[^\]\[]*)\]\s+ # Group "time": 0+ chars other than [ and ], ], 1+ whitespaces
  9. "(?P<request>[^"]*)" # ", Group "request": 0+ non-" chars, "
  10. '''
  11. for item in re.finditer(pattern,logdata,re.VERBOSE):
  12. print(item.groupdict())
Success #stdin #stdout 0.02s 9512KB
stdin
Standard input is empty
stdout
{'host': '159.253.153.40', 'user_name': '-', 'time': '21/Jun/2019:15:46:10 -0700', 'request': 'POST /e-business HTTP/1.0'}
{'host': '136.195.158.6', 'user_name': 'feeney9464', 'time': '21/Jun/2019:15:46:11 -0700', 'request': 'HEAD /open-source/markets HTTP/2.0'}