-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhtml_tag_match.py
More file actions
21 lines (20 loc) · 913 Bytes
/
html_tag_match.py
File metadata and controls
21 lines (20 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# -*- coding: utf-8 -*-
from ArrayStack import ArrayStack
def html_Tag_match(html):
""" Return True if all HTML tags are properly match; False otherwise."""
S = ArrayStack()
j = html.find('<') # find first ’<’ character (if any)
while j != -1:
k = html.find('>', j+1) # find next ’>’ character
if k == -1:
return False # invalid tag
tag = html[j+1:k] # strip away < >
if not tag.startswith('/'): # this is opening tag
S.push(tag)
else: # this is closing tag
if S.is_empty():
return False # nothing to match with
if tag[1:] != S.pop():
return False # mismatched delimiter
j = html.find('<', k+1) # find next ’<’ character (if any)
return S.is_empty() # were all opening tags matched?