改进正则表达式以结束报价

时间:2014-10-28 21:20:20

标签: python regex

我有以下正则表达式:

>>> re.findall('http://www.rottentomatoes.com/.+', html)
['http://www.rottentomatoes.com/m/1129132-torque" class="see-all">Read More About This Movie On Rotten Tomatoes</a>']

如何在"之前将其匹配。我想让回报成为:

http://www.rottentomatoes.com/m/1129132-torque

2 个答案:

答案 0 :(得分:1)

使用非贪婪量词?停在第一个"

>>> html = 'http://www.rottentomatoes.com/m/1129132-torque" class="see-all">Read More About This Movie On Rotten Tomatoes</a>'
>>> re.search('(http://www\.rottentomatoes\.com/.+?)"', html).group(1)
'http://www.rottentomatoes.com/m/1129132-torque'

答案 1 :(得分:0)

只需在要停止的位置添加字符(“)。同时添加?,以便在第一场比赛时停止。

>>> html='http://www.rottentomatoes.com/m/1129132-torque" class="see-all">Read More About This Movie On Rotten Tomatoes</a>'
>>> re.findall('http://www.rottentomatoes.com/.+?\"', html)
['http://www.rottentomatoes.com/m/1129132-torque"']