字符串列表上的字符串处理

时间:2018-10-21 04:13:56

标签: python string list

我有以下字符串列表:

['2018-10-02 00:00:13,824 INFO  [Pop3SSLServer-1631] [ip=192.168.15.15;oip=190.73.158.8;] security - cmd=Auth; account=kshovelbottomhz_30@ucab.edu.ve; protocol=pop3;\n', '2018-10-02 00:00:15,941 INFO  [Pop3SSLServer-1632] [ip=192.168.15.15;oip=209.85.167.142;] security - cmd=Auth; account=ditzakmz@ucab.edu.ve; protocol=pop3;\n', '2018-10-02 00:00:26,352 INFO  [Pop3SSLServer-1629] [ip=192.168.15.15;oip=209.85.160.166;] security - cmd=Auth; account=bfermerox_21@ucab.edu.ve; protocol=pop3;\n', '2018-10-02 00:00:33,751 INFO  [Pop3SSLServer-1630] [ip=192.168.15.15;oip=209.85.208.131;] security - cmd=Auth; account=bhansenim@ucab.edu.ve; protocol=pop3;\n', '2018-10-02 00:00:57,661 INFO  [Pop3SSLServer-1631] [ip=192.168.15.15;oip=209.85.222.159;] security - cmd=Auth; account=afellibrand67@ucab.edu.ve; protocol=pop3;\n', '2018-10-02 00:01:05,645 INFO  [Pop3SSLServer-1632] [ip=192.168.15.15;oip=209.85.161.37;] security - cmd=Auth; account=sdenys9q@ucab.edu.ve; protocol=pop3;\n', '2018-10-02 00:01:18,683 INFO  [Pop3SSLServer-1633] [ip=192.168.15.15;oip=209.85.167.24;] security - cmd=Auth; account=lpidonqc@ucab.edu.ve; protocol=pop3;']

我已经在Python中获得了此列表。我的问题是如何获取每个IP上的特定信息。例如192.168.15.15或列表中剩下的每个IP,例如90.73.158.8、209.85.167.142,等等。出现的每个协议(例如pop3)。谢谢

1 个答案:

答案 0 :(得分:0)

将re用于此任务,并具有积极的前瞻性:

import re
In [1]: a = ['2018-10-02 00:00:13,824 INFO [Pop3SSLServer-1631] [ip=192.168.15.15;oip=190.
   ...: 73.158.8;] security - cmd=Auth; account=kshovelbottomhz_30@ucab.edu.ve; protocol=p
   ...: op3;\n', '2018-10-02 00:00:15,941 INFO [Pop3SSLServer-1632] [ip=192.168.15.15;oip=
   ...: 209.85.167.142;] security - cmd=Auth; account=ditzakmz@ucab.edu.ve; protocol=pop3;
   ...: \n', '2018-10-02 00:00:26,352 INFO [Pop3SSLServer-1629] [ip=192.168.15.15;oip=209.
   ...: 85.160.166;] security - cmd=Auth; account=bfermerox_21@ucab.edu.ve; protocol=pop3;
   ...: \n', '2018-10-02 00:00:33,751 INFO [Pop3SSLServer-1630] [ip=192.168.15.15;oip=209.
   ...: 85.208.131;] security - cmd=Auth; account=bhansenim@ucab.edu.ve; protocol=pop3;\n'
   ...: , '2018-10-02 00:00:57,661 INFO [Pop3SSLServer-1631] [ip=192.168.15.15;oip=209.85.
   ...: 222.159;] security - cmd=Auth; account=afellibrand67@ucab.edu.ve; protocol=pop3;\n
   ...: ', '2018-10-02 00:01:05,645 INFO [Pop3SSLServer-1632] [ip=192.168.15.15;oip=209.85
   ...: .161.37;] security - cmd=Auth; account=sdenys9q@ucab.edu.ve; protocol=pop3;\n', '2
   ...: 018-10-02 00:01:18,683 INFO [Pop3SSLServer-1633] [ip=192.168.15.15;oip=209.85.167.
   ...: 24;] security - cmd=Auth; account=lpidonqc@ucab.edu.ve; protocol=pop3;']

In [2]: for i in a:
   ...:     print(re.findall(r'(?<=ip=)([\d\.]+)', i))
   ...:

['192.168.15.15', '190.73.158.8']
['192.168.15.15', '209.85.167.142']
['192.168.15.15', '209.85.160.166']
['192.168.15.15', '209.85.208.131']
['192.168.15.15', '209.85.222.159']
['192.168.15.15', '209.85.161.37']
['192.168.15.15', '209.85.167.24']