Ejercicio de RegEx #2: Etiquetas HTML

Escribe tres expresiones regulares:

  • Una llamada opening_tags que coincida con todas las etiquetas HTML de apertura, incluidos los atributos.
  • Una llamada closing_tags que coincida con todas las etiquetas HTML de cierre.
  • Una llamada all_tags que coincida con todas las etiquetas HTML, de apertura o de cierre, sus atributos y su contenido (siempre que su contenido esté en la misma línea). Consulta el ejemplo de abajo para ver el resultado esperado.

Ejemplo

index = '''
<html>
<head>
    Hi! I'm a text in the head.
    I probably shouldn't be here.
    <title>edabit.com</title>
</head>
<body>
    Hi! I'm a text in the body.
    <p>This is a parragraph and <a href="https://edabit.com">this is a link</a>.</p>
    Here comes a fake tag: <>.
</body>
</html>
'''

opening_tags = "yourregularexpressionhere"
closing_tags = "yourregularexpressionhere"
all_tags = "yourregularexpressionhere"

re.findall(opening_tags, index) ➞ ["<html>", "<head>", "<title>", "<body>", "<p>", "<a href="https://edabit.com">"]

re.findall(closing_tags, index) ➞ ["</title>", "</head>",  "</a>", "</p>", "</body>", "</html>"]

re.findall(all_tags, index) ➞ ["<html>", "<head>", "<title>edabit.com</title>", "</head>", "<body>", "<p>This is a parragraph and <a href="https://edabit.com">this is a link</a>.</p>", "</body>", "</html>"]

Notas

  • No necesitas escribir una función, solo el patrón.
  • No elimines import re del código.
  • Busca más información sobre RegEx en Recursos.
  • Puedes encontrar todos los desafíos de esta serie en mi colección Basic RegEx.