簡體   English   中英

改進正則表達式以結束報價

[英]Improve regular expression to end on quotation

我有以下正則表達式:

>>> 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

使用非貪婪量詞? 停在第一個"

>>> 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'

只需在要停止的位置添加character(“) 。還要添加? ,使其在第一個匹配項處停止。

>>> 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"']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM