简体   繁体   English

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

[英]Remove only anchor tags from string with regex

I know that to remove all html tags from a string one can use: 我知道从字符串中删除所有html标签可以使用:

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

But is there anyway that I can remove only anchor tags and keep all other tags. 但是无论如何,我只能删除锚标签并保留所有其他标签。 So for example: 因此,例如:

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

become: 成为:

<p>Some text link</p>

It's enough to look for opening and closing a tags separately and omit them: 这足以寻找打开和关闭a单独的标签,并忽略它们:

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

Live demo 现场演示

Thanks revo it worked perfectly. 谢谢revo,它运作良好。 I also manage to fix this problem using this regex 我也设法使用此正则表达式解决此问题

string = re.sub('<a.*?>|</a> ', '', string)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM