Skip to main content
 首页 » 编程设计

python之正则表达式用带有空白的http替换整个字符串

2024年11月24日23soundcode

假设我有一个字符串

string = "Today is going well the dog jumped over the fence. The fence was  
my neighbors asdkhttps://www.urldefence.org/ult/sdfo/90nf3f/lknfksjdn?  
>>>09sdkfjnkbIAS(F89kjfs and here are some more words before another link  
lkm.http//www.someothersite.com/kasd09/3oif/3fo3/3/nffiosuf???  
DF*SDF***8skdjnf and some more words in the end." 

我只想替换包含 http 链接的长字符串,由文本前的空格和文本后的空格定义,http 位于中间的某个位置。

我一直在努力..

re.sub('\bhttps?\b', '', string) 
re.sub('https?.*?', '', string) 
re.sub('https?.*?([\s+])', '', string) 

无法连接点。

请您参考如下方法:

我不太擅长正则表达式,但我尝试使用正则表达式,它与你想要的带空格的字符串相匹配。所以如果可行,您可以试试这个。

http.*?([\s])