首页 > 解决方案 > Python 清理域名 - 正则表达式还是 lambda?

问题描述

我不确定这是否是在 python 中解决此问题的最佳方法。在 bash 中,我可能只使用 awk、sed 并完成它。

根据这篇文章有两个建议,但我无法实施。我想清理域名。

代码

import re

log = ["4/19/2020 11:59:09 PM 2604 PACKET  0000014DE1921330 UDP Rcv 192.168.1.28   f975   Q [0001   D   NOERROR] A      (7)pagead2(17)googlesyndication(3)com(0)",
       "4/19/2020 11:59:09 PM 0574 PACKET  0000014DE18C4720 UDP R cv 192.168.2.54    9c63   Q [0001   D   NOERROR] A      (2)pg(3)cdn(5)viber(3)com(0)"]

rx_dict = {
    'date': re.compile(r'(?P<date>(\d+)[\/](\d+)[\/](\d+))'),
    'time': re.compile(r'(?P<time>\d{2}:\d{2}:\d{2}.(?:AM|PM))'),
    'client': re.compile(r'(?P<client>(?:[0-9]{1,3}\.){3}[0-9]{1,3})'),
    'flags': re.compile(r'(?P<flags>(?<=\].)(.\S{0,}))'),
    'query': re.compile(r'(?P<query>[\S]*)$')
    }

for item in log:
    counter = 0 
    for key, r_exp in rx_dict.items():
        print(f"{r_exp.search(item).group(1)}", end='')
        if counter < 4:
            print(',', end='')
            counter = counter + 1
    print()

输出

4/19/2020,11:59:09 PM,192.168.1.28,A,(7)pagead2(17)googlesyndication(3)com(0)
4/19/2020,11:59:09 PM,192.168.2.54,A,(2)pg(3)cdn(5)viber(3)com(0)

首选输出

4/19/2020,11:59:09 PM,192.168.1.28,A,pagead2.googlesyndication.com
4/19/2020,11:59:09 PM,192.168.2.54,A,pg.cdn.viber.com

标签: pythonregexlambda

解决方案


我假设您想要清理query结果。您可以使用re.sub.

>>> help(re.sub)
Help on function sub in module re:

sub(pattern, repl, string, count=0, flags=0)
   Return the string obtained by replacing the leftmost
   non-overlapping occurrences of the pattern in string by the
   replacement repl.  repl can be either a string or a callable;
   if a string, backslash escapes in it are processed.  If it is
   a callable, it's passed the Match object and must return
   a replacement string to be used.

第一个参数是模式(这里是(AnyNumber))。第二个参数是repl(这里是clean_up_query函数)。每次出现不重叠的模式时都会调用此函数。

>>> import re
>>>
>>> log = [
...     "4/19/2020 11:59:09 PM 2604 PACKET  0000014DE1921330 UDP Rcv 192.168.1.28   f975   Q [0001   D   NOERROR] A      (7)pagead2(17)googlesyndication(3)com(0)",
...     "4/19/2020 11:59:09 PM 0574 PACKET  0000014DE18C4720 UDP R cv 192.168.2.54    9c63   Q [0001   D   NOERROR] A      (2)pg(3)cdn(5)viber(3)com(0)",
... ]
>>>
>>> rx_dict = {
...     "date": re.compile(r"(?P<date>(\d+)[\/](\d+)[\/](\d+))"),
...     "time": re.compile(r"(?P<time>\d{2}:\d{2}:\d{2}.(?:AM|PM))"),
...     "client": re.compile(r"(?P<client>(?:[0-9]{1,3}\.){3}[0-9]{1,3})"),
...     "flags": re.compile(r"(?P<flags>(?<=\].)(.\S{0,}))"),
...     "query": re.compile(r"(?P<query>[\S]*)$"),
... }
>>>
>>> def clean_up_query(match):
...     match_start, match_stop = match.span()
...     if (match_start == 0) or (
...         match_stop == len(match.string)
...     ):  # we do not want "." to be appeared on the result if the match is at the beginning or at the end.
...         return ""
...     return "."
...
>>> for item in log:
...     counter = 0
...     for key, r_exp in rx_dict.items():
...         if key == "query":
...             print(
...                 re.sub(r"\(\d+\)", clean_up_query, r_exp.search(item).group(1)), end=""
...             )
...         else:
...             print(f"{r_exp.search(item).group(1)}", end="")
...         if counter < 4:
...             print(",", end="")
...             counter = counter + 1
...     print()
...
4/19/2020,11:59:09 PM,192.168.1.28,A,pagead2.googlesyndication.com
4/19/2020,11:59:09 PM,192.168.2.54,A,pg.cdn.viber.com

推荐阅读