Advertisement
svetoslavbozov

FTML

Feb 12th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4.  
  5. class Program
  6. {
  7.     static void Main()
  8.     {
  9.         StringBuilder text = new StringBuilder();
  10.         int count = int.Parse(Console.ReadLine());
  11.         string code = null;
  12.  
  13.         while (count > 0)
  14.         {
  15.             code += Console.ReadLine() ;
  16.             code += "\r\n";
  17.             count--;
  18.         }
  19.  
  20.         // regular expressions that match the tags but not nested tags
  21.         string upper = @"(<upper>([a-zA-Z0-9\s!?""]+)</upper>)";
  22.         string lower = @"(<lower>([a-zA-Z0-9\s!?""]+)</lower>)";
  23.         string rev = @"(<rev>([a-zA-Z0-9\s!?""]+)</rev>)";
  24.         string toggle = @"(<toggle>([a-zA-Z0-9\s!?""]+)</toggle>)";
  25.  
  26.         // the cycle continues while there is a match
  27.         while (Regex.IsMatch(code, toggle) || Regex.IsMatch(code, rev)|| Regex.IsMatch(code, lower)||Regex.IsMatch(code, upper))
  28.         {
  29.             code = Regex.Replace(code, upper, m => m.Groups[2].Value.ToUpper()); // sets upper case and deletes tags
  30.             code = Regex.Replace(code, lower, m => m.Groups[2].Value.ToLower()); // sets lower and del tags
  31.  
  32.             if (Regex.IsMatch(code, toggle)) //match toggle
  33.             {
  34.                 Match strTog = Regex.Match(code, toggle); // takes the match
  35.                 string[] split = strTog.ToString().Split(new char[] { '>', '<' }, StringSplitOptions.RemoveEmptyEntries); // splits the match
  36.                 char[] letters = split[1].ToCharArray(); //splits  element[1] from the string because it holds the text after the first split
  37.                 string finalTog = null;
  38.                 foreach (var ch in letters)
  39.                 {
  40.                     finalTog += (char.IsLower(ch) ? char.ToUpper(ch) : char.ToLower(ch)); // toggles the chars in the text
  41.                 }
  42.                 Regex regx = new Regex(toggle);
  43.                 code = regx.Replace(code,finalTog,1); // replaces the old text with toggled text and deletes the tags
  44.             }
  45.             if (Regex.IsMatch(code, rev)) //match reverse
  46.             {
  47.                 Match strRev = Regex.Match(code, rev, RegexOptions.Singleline); // takes the match
  48.                 string[] local = strRev.ToString().Split(new char[] { '>', '<' }, StringSplitOptions.RemoveEmptyEntries); // splits the match
  49.                 char[] localLetters = local[1].ToCharArray(); //splits  element[1] from the string because it holds the text after the first split
  50.                 Array.Reverse(localLetters); //reverse array
  51.                 string finalRev = null;
  52.                 foreach (var ch in localLetters)
  53.                 {
  54.                     finalRev += ch;    //build new string with reversed letters
  55.                 }
  56.                 Regex rgx = new Regex(rev);
  57.                 code = rgx.Replace(code, finalRev, 1); // replase the old text with reversed and delete the tags
  58.             }
  59.         }
  60.         Console.WriteLine(code);
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement