Find text and return list with Regex, Regular Expression

Viewed 69

I am trying to make a list from a text file (.txt) with python regular expression re. Some parts of the text are showing below.

146.204.224.152 - feest6811 [21/Jun/2019:15:45:24 -0700] "POST /incentivize HTTP/1.1" 302 4622\n197.109.77.178 - - kertzmann3129 [21/Jun/2019:15:45:25 -0700] "DELETE /virtual/solutions/target/web+services HTTP/2.0" 203 26554

May I know that how can I regex the text with a list format as:

{
"host_name": "146.204.224.152", 
"name": "feest6811", 
"time": "21/Jun/2019:15:45:24 -0700", 
"method": "POST /incentivize HTTP/1.1"
},
..
..
..

I'm trying to regex with this pattern since I saw example with this pattern:

pattern="(?P<host_name>.*)(\ -\ )(?P<name>\w*)"

for item in re.finditer(pattern,'Text_data',re.VERBOSE):
    print(item.groupdict())

Any suggestion to regex from this text.

1 Answers

Use

(?m)^(?P<host_name>[\d.]+) - (?P<name>\w+) \[(?P<time>[^][]+)] "(?P<method>[^"]+)"

See proof.

Explanation

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?P<host_name>           group and capture to \k<host_name>:
--------------------------------------------------------------------------------
    [\d.]+                   any character of: digits (0-9), '.' (1
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \k<host_name>
--------------------------------------------------------------------------------
   -                       ' - '
--------------------------------------------------------------------------------
  (?P<name>                 group and capture to \k<name>:
--------------------------------------------------------------------------------
    \w+                      word characters (a-z, A-Z, 0-9, _) (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \k<name>
--------------------------------------------------------------------------------
                           ' '
--------------------------------------------------------------------------------
  \[                       '['
--------------------------------------------------------------------------------
  (?P<time>                group and capture to \k<time>:
--------------------------------------------------------------------------------
    [^][]+                   any character except: ']', '[' (1 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \k<time>
--------------------------------------------------------------------------------
  ] "                      '] "'
--------------------------------------------------------------------------------
  (?P<method>                        group and capture to \k<method>:
--------------------------------------------------------------------------------
    [^"]+                    any character except: '"' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \k<method>
--------------------------------------------------------------------------------
  "                        '"'
Related