Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class SemanticalHTML {
- public static void main(String[] args) throws IOException {
- BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
- Pattern openPattern = Pattern.compile("(\\s*)?<div\\s*(.+)?(?:\\s*id\\s*=\\s*\"|\\s*class\\s*=\\s*\")\\s*([^\"]+\\s*)\"\\s*(.+)?>");
- Pattern closePattern = Pattern.compile("(\\s*)?(?:<\\s*\\/div\\s*>)\\s*<(?:[!-]+)\\s*(\\w+)\\s*(?:[-]+)>");
- String line = "";
- StringBuilder stringBuilder = new StringBuilder(line);
- while (!(line = bf.readLine()).equals("END")) {
- Matcher openMatcher = openPattern.matcher(line);
- Matcher closeMatcher = closePattern.matcher(line);
- //open tags
- if (openMatcher.find()) {
- if (openMatcher.group(1) != null) {
- stringBuilder.append(openMatcher.group(1));
- }
- stringBuilder.append("<" + openMatcher.group(3).trim());
- if (openMatcher.group(2) != null) {
- stringBuilder.append(" " + openMatcher.group(2).trim());
- }
- if (openMatcher.group(4) != null) {
- stringBuilder.append(" " + openMatcher.group(4).trim());
- }
- stringBuilder.append(">");
- }
- //close tags
- else if (closeMatcher.find()) {
- String frontPart = "";
- if (closeMatcher.group(1) != null) {
- frontPart = closeMatcher.group(1);
- }
- stringBuilder.append(frontPart);
- stringBuilder.append("</" + closeMatcher.group(2) + ">");
- } else {
- stringBuilder.append(line);
- }
- stringBuilder.append(System.getProperty("line.separator"));
- }
- System.out.println(stringBuilder);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement