0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#fixed the import, just red PEP 8
import re 
###################################################
def extract(data):
    ms = re.match(r'(\S+).*mid:(\d+)' , data) # heure & mid 
    k = re.findall(r"/(\S+)", data) # source & destination  
    exp = result = re.findall(r'NVS:([\w\.]+)',data) # type S & D
    # if the table length is 1 it means that the origin is unknown
    if len(k)==1:
      return {'Heure':ms.group(1), 'mid':ms.group(2),"Origine":"Unknown","Destination":k[0],"Type S":exp[0],"Type D":exp[1]}
    # if the table length it means that there's a a source and a destination
    if len(k)==2:
      return {'Heure':ms.group(1), 'mid':ms.group(2),"Origine":k[0],"Destination":k[1],"Type S":exp[0],"Type D":exp[1]}

问题是,当我有这样的行时,[NVS:FAXG3.1.0/+44614215421]它返回无,有没有办法让第二个停止,NVS:这样它就会停止,就像我们有这样的行data2

data = "13:16:16.146 mta         Messages       I CC Doc O:NVS:SMTP/me@test.no R:NVS:SMTP.0/server@test.de [NVS:FAXG3.1.0/+44614215421] mid:41414"
print extract(data)

它返回

>>>None

data2 = "13:16:16.146 mta         Messages       I CC Doc O:NVS:SMTP/me@test.no R:NVS:SMTP.0/server@test.de  mid:41414"
print extract(data2)

它返回


>>> {'Destination': 'server@test.de', 'mid': '41414', 'Type S': 'SMTP', 'Origine': 'me@test.no', 'Type D': 'SMTP.0', 'Heure': '13:16:16.146'}
4

1 回答 1

1

肮脏的黑客

只需更换

if len(k)==2:

if len(k)>1:

因为它在第二个字符串中找到了超过 2 个匹配项

于 2013-04-26T08:38:06.523 回答