簡體   English   中英

Python:從字符串中刪除URL,URL中包含反斜杠

[英]Python: Remove URL from string, URL containing backslashes

我需要從字符串列表中刪除URL(僅用http替換),但是某些URL中包含反斜杠(\\)。 我在線搜索,發現\\是Python中的轉義字符。 我發現的Stackoverflow答案無濟於事。

s = 'Future Of Education http://twitter.com/A6y2s9Hyys\xa0 Some right, some wrong.'
re.sub(r'http\S+', 'http', s)

我得到的結果是Future Of Education http\\xa0 Some right, some wrong. 而不是我想要的《 Future Of Education http Some right, some wrong. 所以我認為問題是我找不到解決字符串反斜杠的方法嗎?

有什么建議么? 謝謝!

\\xa0不是URL的一部分,它是unicode不間斷空格字符。 您可以將正則表達式更新為http://\\S+以從網址末尾刪除\\xa0

s = 'Future Of Education http://twitter.com/A6y2s9Hyys\xa0 Some right, some wrong.'
print(re.sub(r'http://\S+', 'http', s))

輸出:

Future Of Education http  Some right, some wrong.

感謝@ctwheels提供更新的正則表達式。

暫無
暫無

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

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