Answer the question
In order to leave comments, you need to log in
How to ignore tag content?
I created a program using a stack to check open and closed tags, but the filter marks the tag as correct only in this form: "<>", how can I make it recognize tags of this type ""?
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[len(self.items)-1]
def size(self):
return len(self.items)
def html(tag):
s = Stack()
balanced = True
index = 0
while index < len(tag) and balanced:
symbol = tag[index]
if symbol == "<":
s.push(symbol)
else:
if s.isEmpty():
balanced = False
else:
s.pop()
index = index + 1
if balanced and s.isEmpty():
return True
else:
return False
print(html('<body'))
print(html('</h1>'))
Answer the question
In order to leave comments, you need to log in
import re
s = '<body>'
print re.match('<.*>', s).span()
Use a state machine with three states: the beginning (S1), in the middle without a symbol (S2) and in the middle with a symbol (S3).
In S1, you can only accept an open parenthesis and go to S2.
In S2 you can only accept a simple character and go to S3.
In S3, you can accept simple characters and a closing brace.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question