簡體   English   中英

使用正則表達式僅從字符串中提取鏈接

[英]Extract only the link from string using regex

我想從下面提到的字符串中提取鏈接。

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

我使用以下正則表達式來獲取該鏈接。但是它在“http”之后獲取完整的URL,因為我提到了模式。我想要的是在模式“&sa”之前只獲取URL(即) "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...

這是您需要的正則表達式:

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

在單詞中:在/url?q=之后獲取所有內容/url?q= ,以http://開頭,並且不包含&字符。

暫無
暫無

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

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