Regex将url替换为单词python

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

我正在尝试替换长字符串中的几个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
并使用

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__'