如何在 Python 中删除字符串中的任何 URL [英] How to remove any URL within a string in Python

查看:43
本文介绍了如何在 Python 中删除字符串中的任何 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除字符串中的所有 URL(用"替换它们)我四处寻找,但找不到我想要的东西.

I want to remove all URLs inside a string (replace them with "") I searched around but couldn't really find what I want.

示例:

text1
text2
http://url.com/bla1/blah1/
text3
text4
http://url.com/bla2/blah2/
text5
text6
http://url.com/bla3/blah3/

我希望结果是:

text1
text2
text3
text4
text5
text6

推荐答案

Python 脚本:

import re
text = re.sub(r'^https?:\/\/.*[\r\n]*', '', text, flags=re.MULTILINE)

输出:

text1
text2
text3
text4
text5
text6

此处测试此代码.

Test this code here.

这篇关于如何在 Python 中删除字符串中的任何 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆