How to remove tags from a string in python using regular expressions? (NOT in HTML)

前端 未结 6 1147
终归单人心
终归单人心 2020-12-07 23:22

I need to remove tags from a string in python.

Title

What is the most effici

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 00:21

    If the source text is well-formed XML, you can use the stdlib module ElementTree:

    import xml.etree.ElementTree as ET
    mystring = """Title"""
    element = ET.XML(mystring)
    print element.text  # 'Title'
    

    If the source isn't well-formed, BeautifulSoup is a good suggestion. Using regular expressions to parse tags is not a good idea, as several posters have pointed out.

提交回复
热议问题