0

I have a script that split the informations in a line, i've succeeded to split it and extract the informations i need but i have the slash that shows up :/ i'de like to get only what's after it here's an example:

import re 

data = "12:05:12.121    O:CLASS (SMS:/xxx.xxx@xxx.xx) R:VOICE/1654354 mid:4312" 
ms = re.match(r'(\S+).*mid:(\d+)', data) # extract time and mid 
k = re.findall(r"/\S+", data ) # extract source and destination 
result = { 'time':ms.group(1), 'mid':ms.group(2), "source":k[0],"Destination":k[1]}

print result 

and here's the result {'source': '/xxx.xxx@xxx.xx)', 'Destination':'/1654354', 'mid':'4312','time':'12.05.12.121'}

and the result i want is without slash like here:

{'source': 'xxx.xxx@xxx.xx)', 'Destination':'1654354', 'mid':'4312','time':'12.05.12.121'} 
4

1 回答 1

3

将 包装\S+在一个捕获组中:

k = re.findall(r"/(\S+)", data)

这是使用一个正则表达式获取所有信息的另一种方法:

import re 

data = "12:05:12.121      O:CLASS (SMS:/xxx.xxx@xxx.xx) R:VOICE/1654354 mid:4312" 
result = re.search(r'''
      (?P<time>.*?)
      \s+
      .*?
      \s+
      \(
          (?P<type>.*?):/(?P<source>.*?)
      \)
      \s+
      .*/(?P<destination>\d+)
      \s+
      mid:(?P<mid>\d+)
''', data, re.VERBOSE)

print result.groupdict()
于 2013-04-19T09:15:42.623 回答