Regex to find words between two tags

You can use BeautifulSoup for this HTML parsing.

input = """"<person>John</person>went to<location>London</location>"""
soup = BeautifulSoup(input)
print soup.findAll("person")[0].renderContents()
print soup.findAll("location")[0].renderContents()

Also, it’s not a good practice to use str as a variable name in python as str() means a different thing in python.

By the way, the regex can be:

import re
print re.findall("<person>(.*?)</person>", input, re.DOTALL)
print re.findall("<location>(.*?)</location>", input, re.DOTALL)

Leave a Comment