[Chennaipy] Re module in python

Vijay Kumar B vijaykumar at zilogic.com
Tue Jul 6 07:18:20 EDT 2021


 ---- On Tue, 06 Jul 2021 16:25:55 +0530 Nithya Duraisamy <nithyadurai87 at gmail.com> wrote ----
 > Hi All,
 > Can anyone of your please help me to fix my code using re module?

Hope this code, produces the required output.

import os
import re

txt = [
    '199.72.81.55 - - [01/Jul/1995:00:00:01 -0400] "GET /history/apollo/ HTTP/1.0" 200 6245',
    'unicomp6.unicomp.net - - [01/Jul/1995:00:00:06 -0400] "GET /shuttle/countdown/ HTTP/1.0" 200 3985',
    '199.120.110.21 - - [01/Jul/1995:00:00:09 -0400] "GET /shuttle/missions/sts-73/mission-sts-73.html HTTP/1.0" 200 4085'
]

l1= []
for s in txt:
    m = re.match('.*"([A-Z]+) ((\S+) (\S+))".*', s)
    if m:
        l1.append([m.group(1), m.group(2)])

print(l1)

l2= []
for s in txt:
    l2.append(re.findall("\d+$", s))

print(l2)

Regards,
Vijay


More information about the Chennaipy mailing list