Advertisement
valkata

semanticHTML

Oct 4th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.nio.Buffer;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Scanner;
  8. import java.util.regex.Matcher;
  9. import java.util.regex.Pattern;
  10.  
  11. public class semanticHtml {
  12.     public static void main(String[] args) throws IOException {
  13.         Scanner input = new Scanner(System.in);
  14.         List<String> lines = new ArrayList<>();
  15.         BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  16.  
  17.         while (true) {
  18.             String inputLine = bf.readLine();
  19.             if (inputLine.equals("END")) {
  20.                 break;
  21.             }
  22.             lines.add(inputLine);
  23.         }
  24.  
  25.         Pattern pattern = Pattern.compile("(?<remove><div\\s*(?<front>.+)(class|id)\\s*=\\s*\"(?<target>(main|footer|header|nav|article|section|aside))\"\\s*(?<tail>[^\\s]*)\\s*>)");
  26.         Pattern pattern2 = Pattern.compile("<\\/div>(?<remove1>\\s+<!--\\s*(?<closing>[a-z]+)\\s*-->)");
  27.         for (int i = 0; i < lines.size(); i++) {
  28.             Matcher matcher = pattern.matcher(lines.get(i));
  29.             Matcher matcher2 = pattern2.matcher(lines.get(i));
  30.             if (matcher.find()) {
  31.                 String front = matcher.group("front").trim();
  32.                 String tail = matcher.group("tail").trim();
  33.                 if(front.length() >1){
  34.                     front = " "+front;
  35.                 }
  36.                 if(tail.length() > 1){
  37.                     tail = " " + tail;
  38.                 }
  39.                 String line = lines.get(i)
  40.                         .replace(matcher.group("remove"), "<"
  41.                                 + matcher.group("target") +
  42.                                 front + tail + ">");
  43.                 lines.set(i, line);
  44.             }
  45.             if (matcher2.find()) {
  46.                 String line = lines.get(i)
  47.                         .replace("</div", "</" + matcher2.group("closing"))
  48.                         .replace(matcher2.group("remove1"), "");
  49.                 lines.set(i, line);
  50.             }
  51.         }
  52.         for (String lin : lines) {
  53.             System.out.println(lin);
  54.         }
  55.  
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement