python 正则表达式 替换网址

import re
 
def replace_urls(text):
    # 匹配http或https网址的正则表达式
    url_pattern = re.compile(r'https?://[^\s]+')
    # 替换网址
    return url_pattern.sub('', text)
 
# 示例文本
text = "查看我的网站: https://example.com, 还有我的Github: https://github.com/username"
# 替换网址
new_text = replace_urls(text)
print(new_text)

 

posted @ 2024-08-08 20:29  myrj  阅读(3)  评论(0编辑  收藏  举报