3

I have asked this question before and now i edit it because i found some lines that doesn't correspond to the format i gave before ...


here's an example of the lines:

data = "09:55:04.125 mta         Messages       I Doc O:SERVER (NVS:SMTP/me@domain.com) R:NVS:FAXG3.I0.0101 mid:6393"
data2= "09:55:05.045 mta         Messages       I Doc O:SERVER (NVS:SMTP/me@domain.com) R:ADMIN (NVS:SMTP.0/me@domain.fr) mid:6397"

at first i have matched what's between the slash and two points but i've noticed that there's some lines like the first where the type "FAXG3.I0.0101" isn't followed by a slash


here's the regex i use:

exp = result = re.findall(r'[\w\.]+(?=:*)',data) # type S & D

the result i want is 'SMTP','FAXG3.I0.0101' for the first line and 'SMTP','SMTP.0' for the second. can someone help correcting my regex to get that ??

4

2 回答 2

3

您只需要更改正则表达式,使其也接受“。” 作为有效字符,例如:

import re 
data = "This is a test message I Res O:Myself (KTP:SMTP/me@domain.com) R:KTP:SMS.CLASS/+345854595 id:21"
result = re.findall(r'[\w\.]+(?=:*/)',data)
print result

['SMTP', 'SMS.CLASS']

[\w\.]+表示您将接受由至少一个“任何字母数字字符和下划线”(\w)或.\.-它需要转义,.否则表示“任何字符”)组成的序列。

于 2013-04-23T08:44:17.930 回答
1

那应该工作:

result = re.findall(r'(?<=:)[\w.]+(?=/)',data)

:说“和之间的一系列字母数字字符(或下划线或点)/”。

于 2013-04-23T08:44:28.997 回答