kattaprasanth kattaprasanth - 25 days ago 8
Python Question

Python regular Expression to get text between two strings

when i read a text, i have string like

<h3 class="heading">General Purpose</h3>
in some of the lines of the text, now i want to get only value that is
General Purpose
from above..

d = re.search(re.escape('<h3 class="heading">')+"(.*?)"+re.escape('</h3>'), str(data2))
if d:
print(d.group(0))

Answer

Group 0 contains the entire match; you want the contents of group 1:

print(d.group(1))

But generally, using regexes to parse HTML is not such a good idea (although practically speaking, nested h3 tags should be rather uncommon).