Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在文本中添加html超链接标记站点_Python_Html - Fatal编程技术网

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

我是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">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)
印刷品
印刷品:

随机文本
另一个随机文本:
随机文本