I’m unable to create a list from a Regex match

I’m trying to extract only the “DELETE /[resource]” part from this list:

57.88.37.55 - - [01/Mar/2021:21:50:36 +0000] "DELETE /customers HTTP/1.1" 201 4730
244.240.70.241 - - [01/Mar/2021:21:50:36 +0000] "GET /users HTTP/1.1" 400 3081
16.102.244.72 - - [01/Mar/2021:21:50:36 +0000] "PATCH /users HTTP/1.1" 200 2344
78.7.87.158 - - [01/Mar/2021:21:50:36 +0000] "DELETE /parsers HTTP/1.1" 300 2897
77.51.66.200 - - [01/Mar/2021:21:50:36 +0000] "POST /lists HTTP/1.1" 200 3364
55.12.74.229 - - [01/Mar/2021:21:50:36 +0000] "GET /customers HTTP/1.1" 200 3927
194.115.77.20 - - [01/Mar/2021:21:50:36 +0000] "GET /events HTTP/1.1" 301 2249
95.62.102.64 - - [01/Mar/2021:21:50:36 +0000] "PATCH /events HTTP/1.1" 503 4334
6.36.213.6 - - [01/Mar/2021:21:50:36 +0000] "DELETE /alerts HTTP/1.1" 403 3533

Using the following code:

patternHTTPMethod = re.compile(r'([D]w+s/w+)') #Expressão regular para identificar o método HTTP DELETE e o recurso acedido

lstHTTPMethodDel=[]
for line in fstring: 
    lstHTTPMethodDel.append(patternHTTPMethod.search(line)[0])
print(lstHTTPMethodDel)

(fstring is a variable to which I attributed the list above)

But, alas, I receive the following error:

lstHTTPMethodDel.append(patternHTTPMethod.search(line)[0])
TypeError: 'NoneType' object is not subscriptable

Any ideas, approaches, to this problem?

Answer

Check if the regex matches before accessing the matched string:

patternHTTPMethod = re.compile(r'([D]w+s/w+)')

lstHTTPMethodDel=[]
for line in fstring:
    match = patternHTTPMethod.search(line)
    if match:
        lstHTTPMethodDel.append(match[0])
print(lstHTTPMethodDel)

Leave a Reply

Your email address will not be published. Required fields are marked *