想,我有以下字符串(在Python):如何從字符串中提取url,其中包含協議和地址其餘部分之間的空格?
myString = "For further information please visit http:// somewebpage.com and please do not hesitate to contact us"
我想提取以下網址:
http:// somewebpage.com
我發現使用正則表達式解決方案,而不是一個的情況下地址前留空。
想,我有以下字符串(在Python):如何從字符串中提取url,其中包含協議和地址其餘部分之間的空格?
myString = "For further information please visit http:// somewebpage.com and please do not hesitate to contact us"
我想提取以下網址:
http:// somewebpage.com
我發現使用正則表達式解決方案,而不是一個的情況下地址前留空。
像這樣:
myString = myString.split()
index = myString.index('http://')
url = ''.join(myString[index:index+2])
請注意,我把句子上的每一個字,但只連接http
部分與後的一個。
如果你確實需要的空間(我不能想象爲什麼),然後用替代''
' '
純的正則表達式的解決方案:
http://\s[\w\.]+
[\w\.]
查找任何信件或週期+
尋找上述字符1次或更多次@ LukeBalizet謝謝。這個效果很好。替換正則表達式輸出的空間爲我提供瞭解決方案。 –
試試這個正則表達式:
>>>mystring = "For further information please visit http:// somewebpage.com and please do not hesitate to contact us"
>>>url = re.findall('http[s]?:// (?:[a-zA-Z]|[0-9]|[[email protected]&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', mystring)[0]
>>>url
http:// somewebpage.com
嘗試什麼?我們在這裏尋找什麼? – rick112358
@ rick112358正則表達式從字符串中返回url。 –
/https?:\/\/\s\S+/g
正則表達式將匹配:
http:// somewebpage.com
https:// somewebpage.com
http:// 1234.com/test
但不是:
ftp:// www.test.com.xx
http://www.google.com
http://
哪些協議,你期待? – marvel308
@ marvel308只有http –