正则表达式python解析文件

正则表达式python解析文件,python,regex,python-2.x,Python,Regex,Python 2.x,问题是,当我有这样的行,其中有[NVS:FAXG3.1.0/+44614215421]它返回None,有没有办法让它在第二个NVS:处停止,就像我们有一行data2 #!/usr/bin/env python # -*- coding: utf-8 -*- #fixed the import, just red PEP 8 import re ################################################### def extract(data): m

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

#!/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]}
它回来了

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)

它回来了


肮脏的黑客

替换

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

因为它在第二个字符串中找到了两个以上的匹配项

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'}
if len(k)==2:
if len(k)>1: