2024-09-30 02:24:06 发布
网友
我要删除字符串中的所有URL(将它们替换为“”) 我到处找,但找不到我想要的。
示例:
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
最短的路
re.sub(r'http\S+', '', stringliteral)
这对我有效:
import re thestring = "text1\ntext2\nhttp://url.com/bla1/blah1/\ntext3\ntext4\nhttp://url.com/bla2/blah2/\ntext5\ntext6" URLless_string = re.sub(r'\w+:\/{2}[\d\w-]+(\.[\d\w-]+)*(?:(?:\/[^\s/]*))*', '', thestring) print URLless_string
结果:
Python脚本:
import re text = re.sub(r'^https?:\/\/.*[\r\n]*', '', text, flags=re.MULTILINE)
输出:
测试此代码here。
最短的路
这对我有效:
结果:
Python脚本:
输出:
测试此代码here。
相关问题 更多 >
编程相关推荐