Summary: What regex string would I use to remove tags in a HTML document? Although, this may be a duplicate from a previous answer: How to remove only html tags in a string? and Remove HTML tags in String, I can not programme in those languages fully yet, so this is why I am asking the question.
I am completing a Python Exercise by Google: https://developers.google.com/edu/python/exercises/baby-names it requires you two parse HTML data using regex (the HTML is structured so it is easier). I've been having problems removing the tags surrounding the data:
def extract_names(filename):
"""
Given a file name for baby.html, returns a list starting with the year string
followed by the name-rank strings in alphabetical order.
['2006', 'Aaliyah 91', Aaron 57', 'Abagail 895', ' ...]
"""
# +++your code here+++
#open and read file
file = open(filename,'r')
HTML = file.read()
#html file
#print(HTML)
#extract date
date = re.search(r'(Popularity in )([\d]+)',HTML)
print('Date: ',date.group(2))
#find rank and name remove html tags
ranking_tags = re.findall(r'<td>[\d]</td>',HTML)
rankings = []
name_tags = re.findall(r'<td>[a-z]</td>',HTML,re.IGNORECASE)
names = []
for value in ranking_tags:
rankings.append(re.sub('[<td></td>]','',value))
for value in name_tags:
names.append(re.sub('[<td></td>]','',value))
print(rankings)
print(names)
Currently, my regex does not replace the tags, as they're wrong. I have already tried teaching myself how to remove the tags to no avail: http://www.cbs.dtu.dk/courses/27610/regular-expressions-cheat-sheet-v2.pdf and https://www.tutorialspoint.com/python/python_reg_expressions.htm as well as looking at other sights before writing this.
Any suggestions would be much appreciated.