首页 > 解决方案 > 用 Python 替换一些带有特殊字符的 HTML 标签

问题描述

我有一些包含一些 HTML 标签的字符串,例如<strong>,<em>并且<br/>我需要分别用*,_和替换这些标签\n

例如:

"this string contain <strong>bold</strong> and <em>italic</em> formatted text.<br/>How can I substitute these <strong><em>HTML tags</em></strong> with <strong>Python</strong>?"

必须成为

"this string contain *bold* and _italic_ formatted text.\nHow can I substitute these *_HTML tags_* with *Python*?".

这样做的最佳做法是什么?也许正则表达式?如何?

标签: pythonhtmlregexstringreplace

解决方案


不会python,但是</?strong>*</?em>替换_应该就可以了。

</?strong>匹配<strong> 并且 </strong>因为 ./是可选的?

regex101 上的强示例。


推荐阅读