https://www.acmicpc.net/problem/22859
.
: 모든 문자*
: 0번 이상 반복?
: 비탐욕적 매칭 (가장 짧은 범위를 매칭)<p>Text1</p><p>Text2</p>
<p>.*</p>
: (탐욕적 매칭) 최대 범위를 매칭한다.<p>.*?</p>
: (비탐욕적 매칭) 최소 범위를 매칭한다.import sys
import re
input = lambda: sys.stdin.readline().rstrip()
html = input()
# 1. main parsing
s = len('<main>')
e = len('</main>')
html = html[s : -e]
# 2. div parsing
html = re.sub(r'<div +title="([\w ]*)">', r'title : \1\n', html)
html = re.sub(r'</div>', '', html)
# 3. p parsing
html = re.sub(r'<p>(.*?)</p>', r'\1\n', html)
# 4. p parsing - 모든 태그 지우기
html = re.sub(r'<([\w /]*)>', '', html)
# 5. p parsing - 맨 앞, 맨 뒤 공백 제거
html = re.sub(r' ?\n ?', r'\n', html)
# 6. p parsing - 공백이 2번 이상 나타나면 하나로 대체
html = re.sub(r' {2,}', ' ', html)
print(html)
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String html = br.readLine();
// 1. main parser
int s = "<main>".length();
int e = "</main>".length();
html = html.substring(s, html.length() - e);
// 2. div parser
html = html.replaceAll("<div +title=\"([\\w ]*)\">", "title : $1\n");
html = html.replaceAll("</div>", "");
// 3. p parser
html = html.replaceAll("<p>(.*?)</p>", "$1\n");
// 4. p parser - remove all tag
html = html.replaceAll("<([\\w /]*)>", "");
// 5. p parser - trim
html = html.replaceAll(" ?\n ?", "\n");
// 6. p parser - two space -> one space
html = html.replaceAll(" {2,}", " ");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write(html);
bw.flush();
bw.close();
}
}