你可以使用正则表达式来匹配并替换字符串中的href及其后面的内容。下面是一个示例代码:

import re

s = 'This is a <a href="https://example.com">link</a> to a website.'

# 使用正则表达式匹配href及其后面的内容
pattern = r'<a\s+href="[^"]*">[^<]*</a>'
result = re.sub(pattern, '', s)

print(result)

输出结果为:

This is a  to a website.

这样就成功地使用replace去除了字符串中的href及其后面的内容。

用replace去除href及后面的字符串

原文地址: https://www.cveoy.top/t/topic/ikVO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录