Advertisement
enevlogiev

SemanticHTML

May 28th, 2015
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. class Semantic
  5. {
  6.     static void Main()
  7.     {
  8.         var openTagRegex = new Regex(@"<div.*?((?:id|class)\s*=\s*""(\w+?)"").*?>");
  9.         var closeTagRegex = new Regex(@"<\/div>(\s*<!--\s*(\w+?)\s*-->)");
  10.  
  11.         string input;
  12.         while ((input = Console.ReadLine()) != "END")
  13.         {
  14.             var output = input;
  15.  
  16.             //modify opening tags
  17.             var match = openTagRegex.Match(input);          
  18.             if (match.Success)
  19.             {
  20.                 output = input.Replace("div", match.Groups[2].Value);
  21.                 output = output.Replace(match.Groups[1].Value, "");
  22.                 output = Regex.Replace(output, @"\s+>", ">");
  23.                 output = Regex.Replace(output, @"\s+", " ");
  24.             }
  25.  
  26.             //closing tags
  27.             match = closeTagRegex.Match(input);
  28.             if (match.Success)
  29.             {
  30.                 output = output.Replace(match.Groups[1].Value, "");
  31.                 output = output.Replace("div", match.Groups[2].Value);
  32.             }
  33.  
  34.             Console.WriteLine(output);
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement