# ======= 3 HTML标记匹配 =======
# 实现扩展括号匹配算法,用来检查HTML文档的标记是否匹配。
# HTML标记应该成对、嵌套出现,
# 开标记是<tag>这种形式,闭标记是</tag>这种形式。
#
# 创建一个函数,接受参数为一个字符串,为一个HTML文档中的内容,
# 返回True或False,表示该字符串中的标记是否匹配。
# 输入样例1:
# <html> <head> <title> Example </title> </head> <body> <h1>Hello, world</h1> </body> </html>
思路:首先使用re模块(正则表达式)分离出<>内的字符串,储存倒列表当中。
第二部,遍历列表,如果字符串元素当中没有'/'则直接压入栈,如果有'/'则弹出栈顶元素,判断是否匹配,不匹配则输出False,直到遍历完成。
第三步:如果栈非空,则匹配失败。
import re
def HTMLMatch(s) -> bool:
# 请在此编写你的代码(可删除pass语句)
# 标记之间的匹配关系
words_in_brackets = re.findall(r'<(.*?)>', s)
match_stack = Stack()
for word in words_in_brackets:
if '/' not in word :
match_stack.push(word)
else:
if match_stack.isEmpty() or match_stack.peek() != word[1:]:
return False
else:
match_stack.pop()
if match_stack.isEmpty():
return True
else:
return False
# 调用检验
print("======== 3-HTMLMatch ========")
print(
HTMLMatch(
"<html> <head> <title>Example</title> </head> <body> <h1>Hello, world</h1> </body> </html>"
))
print(
HTMLMatch(
"<html> <head> <title> Test </title> </head> <body> <p>It's just a test.</p> <p>And this is for False.<p> </body> </html>"
))