python - 使用正则表达式仅从字符串中删除 anchor 标记

标签 python regex

我知道要从字符串中删除所有 html 标签,可以使用:

string = re.sub('<[^<]*?/?>', '', string)

但是无论如何我只能删除 anchor 标签并保留所有其他标签。例如:

<p>Some text<a href="#">link</a></p>

成为:

<p>Some text link</p>

最佳答案

单独查找开始和结束a标签并忽略它们就足够了:

<(?:a\b[^>]*>|/a>)

Live demo

关于python - 使用正则表达式仅从字符串中删除 anchor 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42076016/

相关文章:

python - Django 测试 : Test the initial value of a form field

regex - awk 树桩 : regex substitution within a field

javascript - 我解决了与 IE9 相关的正则表达式问题,但我不确定它如何或为何工作

python - Pyserial 没有从 Arduino 获得新的值(value)

python - Django 是否支持在许多非抽象模型之后继承的模型类?

python - 对特定行应用列减法

java - 用特殊字符分割字符串

python - TensorFlow concat 中的排名不匹配错误

javascript - 使用正则表达式获取 CSS 值

node.js - Joi - 无效的正则表达式