Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Text.RegularExpressions;
  8.  
  9. namespace BBcode_Fixer
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             string filename, fileline, matchstring, tempstring = "";
  16.             string concatstring = "";
  17.             Console.Write("Put text file in same location as this app\nEnter text file name (include .txt): ");
  18.             filename = Console.ReadLine();
  19.             Console.WriteLine(filename + "\n");
  20.             try
  21.             {
  22.                 using (StreamReader postcontent = new StreamReader(filename, Encoding.UTF8))
  23.                 {
  24.                     Console.WriteLine("File found!");
  25.                     Regex rx = new Regex(@"\[(td)\].*?\[(\/td)\]", RegexOptions.IgnoreCase | RegexOptions.Multiline);
  26.                     Regex tagfinder = new Regex(@"\[\S*?\]", RegexOptions.IgnoreCase);
  27.                     fileline = postcontent.ReadToEnd();
  28.                     fileline = Regex.Replace(fileline, @"((pt)\])", "px]");
  29.                     fileline = Regex.Replace(fileline, @"\[((table)\])", "[table=noborder]");
  30.                     MatchCollection rawmatches = rx.Matches(fileline);
  31.                     foreach (Match match in rawmatches)
  32.                     {
  33.                         matchstring = match.Value;
  34.                         Console.WriteLine("Old String:\n'{0}' at position {1}",
  35.                               match.Value,
  36.                               match.Index);
  37.                         Console.WriteLine(matchstring + "\n");
  38.                         if (matchstring != "[td][/td]")
  39.                         {
  40.                             matchstring = matchstring.Substring(4, matchstring.Length - 9);
  41.                             matchstring = Regex.Replace(matchstring, @"\[\/.*?\]", "");
  42.                             tempstring = Regex.Replace(matchstring, @"=.*?]", "]");
  43.                             MatchCollection tagmatches = tagfinder.Matches(tempstring);
  44.                             foreach (Match foundtag in tagmatches)
  45.                             {
  46.                                 concatstring = "[/" + foundtag.Value.Substring(1, foundtag.Value.Length-1) + concatstring;
  47.                             }
  48.                             matchstring = "[td]" + matchstring + concatstring + "[/td]";
  49.                             Console.WriteLine(matchstring);
  50.                             fileline = fileline.Replace(match.Value, matchstring);
  51.                             tempstring = "";
  52.                             concatstring = "";
  53.                             matchstring = "";
  54.                         }
  55.                     }
  56.                     postcontent.Close();
  57.                 }
  58.                 StreamWriter writer = new StreamWriter("output.txt");
  59.                 writer.WriteLine(fileline);
  60.                 writer.Close();
  61.             }
  62.             catch (IOException e)
  63.             {
  64.                 Console.WriteLine("The file could not be read:");
  65.                 Console.WriteLine(e.Message);
  66.             }
  67.             Console.ReadKey();
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement