Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Re sub strings any string starting with <a href or simply href

Context: I am removing HTML tags from strings, but for some reason some href tags are still showing up. To avoid this, I’d like to create another re.sub to remove these tags

For example this text:

<a href="/p76643761#p76643761active That's nice

Should be replace with empty string and output should just be That's nice,etc

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Function I have (isn’t removing href tags for some reason)

def remove_html_tags(text):
    text = re.sub('<[^<]+?>', '', text)

    return text

Thank you all!

>Solution :

You could use <a href=\S+\s

def remove_html_tags(text):
    return re.sub(r'<a href=\S+\s', '', text)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading