직접 로직대로 구현을 시도해보다가 포기했다.
# 정규표현식
import re
html = input()
contents = re.findall(r"<\w+>", html)
is_tag_started = False
tag = None
answer = ""
index = 0
html_length = len(html)
while index < html_length:
if html[index] == "<":
is_tag_started = True
if is_tag_started:
current_tag = ""
while html[index + 1] != ">":
index += 1
current_tag += html[index]
is_tag_started = False
# 현재 타이틀 찾기
if current_tag == "div":
current_title = ""
while html[index + 1] == "'" or html[index + 1] == '"':
index += 1
while html[index + 1] != "'" or html[index + 1] != '"':
current_title += html[index + 1]
index += 1
answer += f"title : {current_title}\n"
정규표현식을 사용할 수 있겠다는 것은 알았는데, 문제는 어떻게 사용하는지를 몰랐다. 정규표현식을 사용한 깔끔한 다른 풀이를 참고하여 해결했다.
import re
html = input()
html = html[len("<main>") : -len("</main>")]
html = re.sub(r'<div +title="([\w ]*)">', r"title : \1\n", html)
html = re.sub(r"</div>", "", html)
html = re.sub(r"<p>(.*?)</p>", r"\1\n", html)
html = re.sub(r"<([\w /]*)>", "", html)
html = re.sub(r" ?\n ?", r"\n", html)
html = re.sub(r" {2,}", " ", html)
print(html)
'알고리즘' 카테고리의 다른 글
[백준/파이썬] #2294 (0) | 2025.03.19 |
---|---|
[백준/파이썬] #22860 (0) | 2025.03.18 |
[백준/파이썬] #22856 (0) | 2025.03.16 |
[백준/파이썬] #20164 (0) | 2025.03.14 |
[백준/파이썬] #14719 (0) | 2025.03.13 |