Muh*_*aha 31 python regex url replace python-2.7
我想删除字符串中的所有URL(用""替换它们)我搜索过但却找不到我想要的东西.
例:
text1
text2
http://url.com/bla1/blah1/
text3
text4
http://url.com/bla2/blah2/
text5
text6
http://url.com/bla3/blah3/
Run Code Online (Sandbox Code Playgroud)
我希望结果如下:
text1
text2
text3
text4
text5
text6
Run Code Online (Sandbox Code Playgroud)
Ωme*_*ega 56
Python脚本:
import re
text = re.sub(r'^https?:\/\/.*[\r\n]*', '', text, flags=re.MULTILINE)
Run Code Online (Sandbox Code Playgroud)
输出:
text1
text2
text3
text4
text5
text6
Run Code Online (Sandbox Code Playgroud)
在这里测试此代码.
tol*_*maz 30
最短的路
re.sub(r'http\S+', '', stringliteral)
Run Code Online (Sandbox Code Playgroud)
Muh*_*aha 16
这对我有用:
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
Run Code Online (Sandbox Code Playgroud)
结果:
text1
text2
text3
text4
text5
text6
Run Code Online (Sandbox Code Playgroud)
Sam*_*Nde 11
您真正想要做的是删除以任何一个http://
或https://
加上非空白字符的任意组合开头的任何字符串。这是我将如何解决它。我的解决方案与@tolgayilmaz 的解决方案非常相似
#Define the text from which you want to replace the url with "".
text ='''The link to this post is /sf/ask/793238771/'''
import re
#Either use:
re.sub('http://\S+|https://\S+', '', text)
#OR
re.sub('http[s]?://\S+', '', text)
Run Code Online (Sandbox Code Playgroud)
运行上面任一代码的结果是
>>> 'The link to this post is '
Run Code Online (Sandbox Code Playgroud)
我更喜欢第二个,因为它更具可读性。
使用正则表达式应该很简单.您可以通过re
python中的模块使用它们.
对于哪个正则表达式可以最好地检测有效URL,请检查以下SO问题:
这些中有很多高度评价的答案,所以应该给你一些方向.
小智 7
此解决方案适用于http,https和其他普通的url类型特殊字符:
import re
def remove_urls (vTEXT):
vTEXT = re.sub(r'(https|http)?:\/\/(\w|\.|\/|\?|\=|\&|\%)*\b', '', vTEXT, flags=re.MULTILINE)
return(vTEXT)
print( remove_urls("this is a test https://sdfs.sdfsdf.com/sdfsdf/sdfsdf/sd/sdfsdfs?bob=%20tree&jef=man lets see this too https://sdfsdf.fdf.com/sdf/f end"))
Run Code Online (Sandbox Code Playgroud)
我知道这已经得到了回答,而且很愚蠢,但我认为这应该在这里。这是一个匹配任何类型 url 的正则表达式。
[^ ]+\.[^ ]+
Run Code Online (Sandbox Code Playgroud)
它可以像
re.sub('[^ ]+\.[^ ]+','',sentence)
Run Code Online (Sandbox Code Playgroud)
import re
re.sub(r'''(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))''', " ", text)
Run Code Online (Sandbox Code Playgroud)
为了消除任何URL一个内Python中的字符串,就可以使用这个表达式功能:
import re
def remove_URL(text):
"""Remove URLs from a text string"""
return re.sub(r"http\S+", "", text)
Run Code Online (Sandbox Code Playgroud)
您也可以从相反的角度来看它...
from urlparse import urlparse
[el for el in ['text1', 'FTP://somewhere.com', 'text2', 'http://blah.com:8080/foo/bar#header'] if not urlparse(el).scheme]
Run Code Online (Sandbox Code Playgroud)
我找不到能处理我的特殊情况的任何东西,即删除推文中间的URL,而这些推文的中间也有空格,所以我自己做了:
(https?:\/\/)(\s)*(www\.)?(\s)*((\w|\s)+\.)*([\w\-\s]+\/)*([\w\-]+)((\?)?[\w\s]*=\s*[\w\%&]*)*
Run Code Online (Sandbox Code Playgroud)
这是一种解释:
(https?:\/\/)
匹配http://或https://
(\s)*
可选空格
(www\.)?
可选匹配www。
(\s)*
可选地匹配空格,
((\w|\s)+\.)*
匹配一个或多个单词字符
([\w\-\s]+\/)*
中的0个或多个,后跟一个句点,匹配一个或多个单词(或破折号或空格)中的0个或多个,
([\w\-]+)
并在URL末尾加上“ \” 可选的结尾
((\?)?[\w\s]*=\s*[\w\%&]*)*
匹配结尾查询参数(即使有空格等)
在这里测试:https : //regex101.com/r/NmVGOo/8
归档时间: |
|
查看次数: |
69549 次 |
最近记录: |