Exercício de RegEx #2: Tags HTML

Escreva três expressões regulares:

  • Uma chamada opening_tags que corresponda a todas as tags HTML de abertura, incluindo os atributos.
  • Uma chamada closing_tags que corresponda a todas as tags HTML de fechamento.
  • Uma chamada all_tags que corresponda a todas as tags HTML, de abertura ou fechamento, seus atributos e seu conteúdo (desde que o conteúdo esteja na mesma linha). Consulte o exemplo abaixo para ver o resultado esperado.

Exemplo

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

  • Você não precisa escrever uma função, apenas o padrão.
  • Não remova import re do código.
  • Encontre mais informações sobre RegEx em Recursos.
  • Você pode encontrar todos os desafios desta série na minha coleção Basic RegEx.