Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fetch data using regex

Tags:

python

regex

I am trying to fetch data using regex

logdata='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'
pattern="""(?P<host>.*)( - \ )(?P<user_name>\w*)"""
for item in re.finditer(pattern,logdata,re.VERBOSE):
    print(item.groupdict())

The output looks like

{'host': '146.204.224.152 ', 'user_name': 'feest6811'}
{'host': '197.109.77.178 ', 'user_name': 'kertzmann3129'}

But I want

{'host': '146.204.224.152', 'user_name': 'feest6811', 'time': '21/Jun/2019:15:45:24 -0700', 'request': 'POST /incentivize HTTP/1.1'}
{'host': '197.109.77.178', 'user_name': 'kertzmann3129', 'time': '21/Jun/2019:15:45:25 -0700', 'request': 'DELETE /virtual/solutions/target/web+services HTTP/2.0'}
like image 971
Kanika Singhal Avatar asked Apr 20 '26 15:04

Kanika Singhal


1 Answers

You can use

r'(?P<host>[\d.]+)\ -\ (?P<user_name>\w+)\ \[(?P<time>[^][]+)]\ "(?P<request>[^"]+)"'

See the regex demo

Details

  • (?P<host>[\d.]+) - Group "host": one or more digits / dots
  • \ -\ - a string
  • (?P<user_name>\w+) - Group "user_name": one or more word chars
  • \ \[ - space + [
  • (?P<time>[^][]+) - Group "time": one or more chars other than ] and [
  • ]\ " - ] " substring
  • (?P<request>[^"]+) - Group "request": one or more chars other than a "
  • " - a " char.
like image 197
Wiktor Stribiżew Avatar answered Apr 23 '26 05:04

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!