使用python从`:`提取数据

时间:2015-03-30 18:06:20

标签: python regex

Page load for http://xxxx?roxy=www.yahoo.com&eventto=https://mywebsite?event took 4001 ms (Ne: 167 ms, Se: 2509 ms, Xe: 1325 ms)<br><br><br>Topic: Yahoo!! My website is a good website | Mywebsite<br>

我想在python中使用正则表达式从上面的消息中提取Topic。我希望提取的消息为Yahoo!! My website is a good website | Mywebsite

2 个答案:

答案 0 :(得分:1)

您可以尝试使用RegEx r'Topic: (.*)\<br\>'

>>> s = 'Page load for http://xxxx?roxy=www.yahoo.com&eventto=https://mywebsite?event took 4001 ms (Ne: 167 ms, Se: 2509 ms, Xe: 1325 ms)<br><br><br>Topic: Yahoo!! My website is a good website | Mywebsite<br>'
>>> import re
>>> re.search(r'Topic: (.*)\<br\>',s).group(1)
'Yahoo!! My website is a good website | Mywebsite'

注意:使用字符串处理比使用正则表达式

可以更快地完成此操作

答案 1 :(得分:1)

如果您知道分隔符,则可以拆分字符串。

>>> s.split('Topic: ')[1].split('<br>')[0]
'Yahoo!! My website is a good website | Mywebsite'