python - Python - 将文本链接转换为 html
问题描述
def linkify(msg):
for m in msg.split(" "):
m = m.lower()
if m.startswith("https:") or m.startswith("http:"):
msg = re.sub(m, f"<a href='{m}' target='_blank'>{m}</a>", msg, re.I)
return msg
我想要做的是将普通链接制作成 html 链接。虽然忽略了用户可能会或可能不会发送的 html 链接。我正在使用这个测试字符串:hi how are you https://google.com/img https://google.com/img/l <a href='https://google.com/img' target='_blank'>https://google.com/img</a> hi <a href='http://test.com/img' target='_blank'>http://test.com/img</a> <a href='https://test.com/hi/lol' target='_blank'>https://test.com/hi/lol</a>
输出
hi how are you <a href='https://google.com/img' target='_blank'>https://google.com/img</a> <a href='https://google.com/img' target='_blank'>https://google.com/img</a>/l <a href='https://google.com/img' target='_blank'>https://google.com/img</a> hi <a href='http://test.com/img' target='_blank'>http://test.com/img</a> <a href='https://test.com/hi/lol' target='_blank'>https://test.com/hi/lol</a>
注意链接:<a href='https://google.com/img' target='_blank'>https://google.com/img</a>/l
被/1
完全忽略,我不知道为什么
期望的输出
<a href='https://google.com/img' target='_blank'>https://google.com/img</a> <a href='https://google.com/img/1' target='_blank'>https://google.com/img/1</a> <a href='https://google.com/img' target='_blank'>https://google.com/img</a> hi <a href='http://test.com/img' target='_blank'>http://test.com/img</a> <a href='https://test.com/hi/lol' target='_blank'>https://test.com/hi/lol</a>
解决方案
您可以使用
import regex as re
text = """hi how are you https://google.com/img https://google.com/img/l <a href='https://google.com/img' target='_blank'>https://google.com/img</a> hi <a href='http://test.com/img' target='_blank'>http://test.com/img</a> <a href='https://test.com/hi/lol' target='_blank'>https://test.com/hi/lol</a>"""
rx = re.compile(r'<a.*?</a>(*SKIP)(*FAIL)|\bhttps?://\S+')
text = rx.sub(lambda m: f"<a href='{m.group(0)}' target='_blank'>{m.group(0)}</a>", text)
print(text)
哪个产量
hi how are you <a href='https://google.com/img' target='_blank'>https://google.com/img</a> <a href='https://google.com/img/l' target='_blank'>https://google.com/img/l</a> <a href='https://google.com/img' target='_blank'>https://google.com/img</a> hi <a href='http://test.com/img' target='_blank'>http://test.com/img</a> <a href='https://test.com/hi/lol' target='_blank'>https://test.com/hi/lol</a>
请参阅regex101.com 上的表达式演示。
推荐阅读
- vue.js - 使用单选按钮通过 Vue.js 设置项目的状态
- mysql - AWS Cloudwatch DatabaseConnections 指标不同于 mysql Threads_connected
- php - Laravel:@can 在查询生成器上抛出错误?
- java - 根据环境改变方法
- wpf - 在 MVVM 中取消 LostFocus
- javascript - 在包含数组元素的对象上使用 .reduce 将其转换为数组
- php - 消息:在 Web 服务器上执行 python selenium 时,“chromedriver”可执行文件需要在 PATH 中
- azure-sql-database - 恢复旧数据库,现在 Umbraco 连接字符串不起作用
- javascript - 单击链接时如何在Materialize CSS中隐藏sidenav?
- font-awesome - Font Awesome css 表包含内容旁边的所有方块: