How to split html tag string using regex?

by lindsey.homenick , in category: Third Party Scripts , a month ago

How to split html tag string using regex?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lottie , a month ago

@lindsey.homenick 

You can split HTML tag string using regular expressions in Python. Here is an example:

1
2
3
4
5
6
7
import re

html_string = "<div><p>Hello</p><a href='example.com'>Link</a></div>"
tags = re.findall(r'<[^>]+>', html_string)

for tag in tags:
    print(tag)


In this example, we are using the re.findall() function to find all substrings that match the specified regular expression <[^>]+>. This regular expression matches any content within angle brackets, which in this case represents HTML tags. The tags list will contain all the HTML tags found in the html_string, and you can further manipulate or process each tag as needed.