Python 在文本中添加html超链接标记站点
我是Python新手,正在研究从纯文本到“可读”html的转换。 所以,除了制作超链接外,我几乎制作了所有超链接 因此,我需要查找和替换文本链接,有一个示例: 输入:Python 在文本中添加html超链接标记站点,python,html,Python,Html,我是Python新手,正在研究从纯文本到“可读”html的转换。 所以,除了制作超链接外,我几乎制作了所有超链接 因此,我需要查找和替换文本链接,有一个示例: 输入: random_text another_random_text: https://example.com/random_text random_text 输出: random_text another_random_text: <a href="https://example.com/random_text&qu
random_text
another_random_text: https://example.com/random_text
random_text
输出:
random_text
another_random_text: <a href="https://example.com/random_text">example.com</a>
random_text
random_text
another_random_text: <a href="https://example.com/random_text">example.com</a>
random_text
随机文本
另一个随机文本:
随机文本
我被它绊住了,不知道如何检测链接并正确地剪切它您可以使用正则表达式执行任务()。例如:
import re
s = '''
random_text
another_random_text: https://example.com/random_text
random_text
'''
s = re.sub(r'(https?://([^/]+)(?:[^\s]*))', r'<a href="\1">\2</a>', s)
print(s)
重新导入
s='''
随机文本
另一个随机文本:https://example.com/random_text
随机文本
'''
s=re.sub(r'(https?:/([^/]+)(?:[^\s]*),r'',s)
印刷品
印刷品:
随机文本
另一个随机文本:
随机文本