Regex将url替换为单词python
我正在尝试替换长字符串中的几个URL 这里有一个例子:Regex将url替换为单词python,python,regex,url,Python,Regex,Url,我正在尝试替换长字符串中的几个URL 这里有一个例子: s = 'https://www.yellowpages.ca/bus/Alberta/Edmonton/MNS-Enterprise-\nLtd/8114324.html, https://411.ca/business/profile/13300641' 由于url中的换行符,匹配将始终在\n处停止。 我试过了 但是结果在\n 'website__\nLtd/8114324.html, website__' 您可以添加\n并使用 r
s = 'https://www.yellowpages.ca/bus/Alberta/Edmonton/MNS-Enterprise-\nLtd/8114324.html, https://411.ca/business/profile/13300641'
由于url中的换行符,匹配将始终在\n
处停止。
我试过了
但是结果在\n
'website__\nLtd/8114324.html, website__'
您可以添加
\n
并使用
re.sub(r'https?://[\n\S]+\b','',S)
看。详情:
-https?://
或http://
https://
-一个或多个换行符或非空白字符[\n\S]+
-直到最右边的单词边界\b
重新导入
s=https://www.yellowpages.ca/bus/Alberta/Edmonton/MNS-Enterprise-\nLtd/8114324.html,https://411.ca/business/profile/13300641'
打印(re.sub(r'https?://[\n\S]+\b',“网站”)
#=>网站,网站__
'website__\nLtd/8114324.html, website__'