How to split html tag string using regex?

by lindsey.homenick , in category: Third Party Scripts , 4 months ago

How to split html tag string using regex?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lottie , 4 months 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.

Related Threads:

How to split a string using regex?
How to split string into parts using regex?
How to remove html tags from an html string using regex?
How to split a string in oracle using substr/instr?
How to split city, state, country string using postgresql?
How to inject meta tag in html using webpack?