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]}

well the issue is that when i have lines like that where there's [NVS:FAXG3.1.0/+44614215421] it returns None, is there way to make is stop at the second NVS: so that it stopps like if we have a line like data2

data = "13:16:16.146 mta         Messages       I CC Doc O:NVS:SMTP/[email protected] R:NVS:SMTP.0/[email protected] [NVS:FAXG3.1.0/+44614215421] mid:41414"
print extract(data)

It returns

>>>None

data2 = "13:16:16.146 mta         Messages       I CC Doc O:NVS:SMTP/[email protected] R:NVS:SMTP.0/[email protected]  mid:41414"
print extract(data2)

It returns


>>> {'Destination': '[email protected]', 'mid': '41414', 'Type S': 'SMTP', 'Origine': '[email protected]', 'Type D': 'SMTP.0', 'Heure': '13:16:16.146'}

1 Answer 1

1

Dirty hack

Just replace

if len(k)==2:

to

if len(k)>1:

because it found more than 2 matches in the second string

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.