I am going to replace some keywords with urls in a string, for example,
content.replace("Google","<a href="http://www.google.com">Google</a>")
However, I only want to replace keywords with urls ONLY if not already wrapped in a url.
The content is simple HTML:
<p><b>This is an example!</b></p><p>I love <a href="http://www.google.com">Google</a></p><p><a href="http://www.google.com"><img src="/google.jpg" /></a></p>
Mainly <a>
and <img>
tags.
The main question: How to determine if a keyword is already wrapped in a <a>
or <img>
tag?
Here is a similar question in PHP find and replace keywords with urls ONLY if not already wrapped in a url, but the answer is not an efficient one.
Is there some better solutions in Python? Better with code examples. Thanks!
This is an example!
I love Google
![]()
` – Susan Mayer Jun 09 '12 at 11:32