2014-02-25 36 views
0

我想從下面提到的字符串中提取鏈接。只使用正則表達式從字符串中提取鏈接

str = /url?q=http://www.example.com/services/blog/first-article&sa=U&ei... 

我用下面的正則表達式來獲取link.But之後的「http」獲取完整的URL,因爲我提到be.What我想要的圖案,該圖案之前,只得到URL「& SA 「(即)"http://www.example.com/services/blog/first-article"

links = re.findall(r'/url\?q=(http://.*)', str) 
print links # http:example.com/services/blog/first-article&sa=U&ei... 
+0

爲什麼不'r'/ url \?q =(http://.*?)&sa =。*''? – RedX

+0

感謝它的工作。 – user

回答

2

這是正則表達式,你需要:

links = re.findall(r'/url\?q=(http://[^&]*)', str) 

在口頭上:後得到的一切,從http://開始,並且不包含&字符。

相關問題