我有一个脚本将信息分成一行,我已经成功分割它并提取我需要的信息,但我有斜线显示:/我想只得到它后面的东西这里是一个例如:
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
这是结果{'source': '/xxx.xxx@xxx.xx)', 'Destination':'/1654354', 'mid':'4312','time':'12.05.12.121'}
我想要的结果就像这里没有斜线:
{'source': 'xxx.xxx@xxx.xx)', 'Destination':'1654354', 'mid':'4312','time':'12.05.12.121'}
答案 0 :(得分: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()