2 回答

TA貢獻1868條經(jīng)驗 獲得超4個贊
這是使用簡單循環(huán)的簡單易懂的方法:
str = '<!DOCTYPE html><html>example<head>hello<meta charset="utf-8">'
words = []
temp = ""
flag = 0
for i in str:
if i=="<":
flag = 0
if temp:
words.append(temp)
temp = ""
elif i==">":
flag=1
else:
if flag==1:
temp += i
print(words) # prints ['example', 'hello']

TA貢獻1757條經(jīng)驗 獲得超8個贊
將變量初始化tag_depth為零。
一次迭代一個字符的字符串。如果您看到一個<字符,則增加tag_depth,如果您看到一個>字符,則減少它。如果看到任何其他字符且tag_depth為零,則輸出該字符。
tag_depth = 0
for c in mystring:
if c == '<':
tag_depth += 1
elif c == '>':
tag_depth -= 1
elif tag_depth == 0:
print(f"{c}", end=0)
添加回答
舉報