Какое регулярное выражение может соответствовать этому шаблону? - PullRequest
2 голосов
/ 05 марта 2020

У меня есть следующий вывод:

T 2020/03/05 16:06:41.565817 193.126.13.199:80 -> 10.8.0.4:55639 [AP] HTTP/1.1 200 OK..Date: Thu, 05 Mar 2020 16:06:41 GMT..Server: Apache/2.2.3 (CentOS)..Expires: Thu, 19 Nov 1981 08:52:00 GMT..Cache-Control: no-store, no-cache, 
T 2020/03/05 16:06:46.727199 10.8.0.4:55642 -> 193.126.13.199:80 [AP] GET / HTTP/1.1..Host: www.radionova.fm..User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:72.0) Gecko/xml;q=0.9,image/webp,*/*;q=0.8..Accept-Langu
T 2020/03/05 16:06:47.174078 193.126.13.199:80 -> 10.8.0.4:55642 [A] HTTP/1.1 200 OK..Date: Thu, 05 Mar 2020 16:06:46 GMT..Server: Apache/2.2.3 (CentOS)..Expires: Thu, 19 Nov 1981 08:52:00 GMT..Cache-Control: no-store, no-cache

Как сделать шаблон регулярного выражения, чтобы он соответствовал только строкам [AP]?

Что-то вроде:

T 2020/03/05 16: 06: 46,727199 10.8.0.4: 55642 -> 193.126.13.199: 80 [AP] GET / HTTP / 1.1..Host: www.radionova.fm..User-Agent: Mozilla / 5.0 (Macintosh; Intel Ma c OS X 10.15; rv: 72.0)

Итак ... первая группа: 2020/03/05

Вторая группа: 16: 06: 46.727199

Третья группа: 10.8.0.4:55642

Четвертая группа: GET / HTTP / 1.1..Host: www.radionova.fm..User-Agent: Mozilla / 5.0 (Macintosh; Intel Ma c OS X 10.15; rv: 72.0)

У меня есть следующее python регулярное выражение:

pattern = r'''T\s([^ ]+)\s([^ ]+)\s([^ ]+).*?[.]{2,}(.*?)[.]{2,}'''

Не работает, как я хочу ..

Ответы [ 2 ]

1 голос
/ 05 марта 2020

Почему не очевидный оператор in?

data = """
T 2020/03/05 16:06:41.565817 193.126.13.199:80 -> 10.8.0.4:55639 [AP] HTTP/1.1 200 OK..Date: Thu, 05 Mar 2020 16:06:41 GMT..Server: Apache/2.2.3 (CentOS)..Expires: Thu, 19 Nov 1981 08:52:00 GMT..Cache-Control: no-store, no-cache, 
T 2020/03/05 16:06:46.727199 10.8.0.4:55642 -> 193.126.13.199:80 [AP] GET / HTTP/1.1..Host: www.radionova.fm..User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:72.0) Gecko/xml;q=0.9,image/webp,*/*;q=0.8..Accept-Langu
T 2020/03/05 16:06:47.174078 193.126.13.199:80 -> 10.8.0.4:55642 [A] HTTP/1.1 200 OK..Date: Thu, 05 Mar 2020 16:06:46 GMT..Server: Apache/2.2.3 (CentOS)..Expires: Thu, 19 Nov 1981 08:52:00 GMT..Cache-Control: no-store, no-cache
"""

rows_ap = [(splitted[1], splitted[2], splitted[3], " ".join(splitted[7:]))
           for line in data.split("\n")
           if line and "[AP]" in line
           for splitted in [line.split(" ")]]

print(rows_ap)
0 голосов
/ 05 марта 2020

Вы можете добавить соответствующие 2 дополнительные части, соответствующие пробельным и непробельным символам и соответствующие \[AP] part

 T\s(\S+)\s(\S+)\s(\S+)\s\S+\s\S+\s\[AP].*?\.{2}(.*?)\.{2}.*

Regex demo | Python демо

import re

regex = r"T\s(\S+)\s(\S+)\s(\S+)\s\S+\s\S+\s\[AP].*?\.{2}(.*?)\.{2}.*"

test_str = ("T 2020/03/05 16:06:41.565817 193.126.13.199:80 -> 10.8.0.4:55639 [AP] HTTP/1.1 200 OK..Date: Thu, 05 Mar 2020 16:06:41 GMT..Server: Apache/2.2.3 (CentOS)..Expires: Thu, 19 Nov 1981 08:52:00 GMT..Cache-Control: no-store, no-cache, \n"
    "T 2020/03/05 16:06:46.727199 10.8.0.4:55642 -> 193.126.13.199:80 [AP] GET / HTTP/1.1..Host: www.radionova.fm..User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:72.0) Gecko/xml;q=0.9,image/webp,*/*;q=0.8..Accept-Langu\n"
    "T 2020/03/05 16:06:47.174078 193.126.13.199:80 -> 10.8.0.4:55642 [A] HTTP/1.1 200 OK..Date: Thu, 05 Mar 2020 16:06:46 GMT..Server: Apache/2.2.3 (CentOS)..Expires: Thu, 19 Nov 1981 08:52:00 GMT..Cache-Control: no-store, no-cache")

matches = re.finditer(regex, test_str)

for matchNum, match in enumerate(matches, start=1):
    print (match.group())

Выход

T 2020/03/05 16:06:41.565817 193.126.13.199:80 -> 10.8.0.4:55639 [AP] HTTP/1.1 200 OK..Date: Thu, 05 Mar 2020 16:06:41 GMT..Server: Apache/2.2.3 (CentOS)..Expires: Thu, 19 Nov 1981 08:52:00 GMT..Cache-Control: no-store, no-cache, 
T 2020/03/05 16:06:46.727199 10.8.0.4:55642 -> 193.126.13.199:80 [AP] GET / HTTP/1.1..Host: www.radionova.fm..User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:72.0) Gecko/xml;q=0.9,image/webp,*/*;q=0.8..Accept-Langu
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...