I
I
igoodmood2016-02-27 19:17:15
Python
igoodmood, 2016-02-27 19:17:15

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

2 answer(s)
D
Dmitry Voronkov, 2016-02-27
@DmitryVoronkov

import re

s = '<body>'
print re.match('<.*>', s).span()

A
abcd0x00, 2016-02-28
@abcd0x00

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 question

Ask a Question

731 491 924 answers to any question