Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove only anchor tags from string with regex

Tags:

python

regex

I know that to remove all html tags from a string one can use:

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>
like image 976
Toni Joe Avatar asked Oct 19 '25 21:10

Toni Joe


1 Answers

It's enough to look for opening and closing a tags separately and omit them:

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

Live demo

like image 89
revo Avatar answered Oct 21 '25 11:10

revo