Advertisement
Guest User

ConvertTextTable

a guest
Feb 26th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.94 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. using System.IO;
  5.  
  6. namespace ConvertTextTable
  7. {
  8.   /// <summary>
  9.   /// Converting text-like tables to proper CSV format.
  10.   /// </summary>
  11.   class ConvertTextTableProcedure
  12.   {
  13.     /// <summary>
  14.     /// Path of the source file.
  15.     /// </summary>
  16.     const string sourceFilePath = @"C:\Data\MyTable.txt";
  17.  
  18.     /// <summary>
  19.     /// Path of the destination file.
  20.     /// </summary>
  21.     const string destinationFilePath = @"C:\Data\Table.csv";
  22.  
  23.     /// <summary>
  24.     /// Main function of application.
  25.     /// </summary>
  26.     /// <param name="args">Array of input parameters.</param>
  27.     static void Main(string[] args)
  28.     {
  29.       var file = File.ReadAllText(sourceFilePath);
  30.       var searchPattern = @"[A-Za-z0-9/]+(\s|\.)+([a-záéíóöőúüű]+\s+)+([0-9]*\s*)[0-9]+(\.)[0-9]+(\s+)[0-9]+(\.)[0-9]+"
  31.                            + @"(\s+)([a-záéíóöőúüű]+\s+)*(---|___)";
  32.       var dataLine = Regex.Match(file, searchPattern);
  33.  
  34.       File.WriteAllText(destinationFilePath,
  35.                         (@"identifier; name; first number; second number; third number" + Environment.NewLine));
  36.  
  37.       while (dataLine.Success)
  38.       {
  39.         var identifier = Regex.Match(dataLine.Value, @"[A-Za-z0-9/]+");
  40.         var startPosition = identifier.Index + identifier.Length;
  41.         var nameFirstPart = Regex.Match(dataLine.Value.Substring(startPosition), @"([a-záéíóöőúüű]+\s+?)+");
  42.         startPosition += nameFirstPart.Index + nameFirstPart.Length;
  43.         var firstNumber = Regex.Match(dataLine.Value.Substring(startPosition), @"\s[0-9]+\s+?");
  44.         startPosition += firstNumber.Index + firstNumber.Length;
  45.         var secondNumber = Regex.Match(dataLine.Value.Substring(startPosition), @"[0-9]+(\.)[0-9]+\s+?");
  46.         startPosition += secondNumber.Index + secondNumber.Length;
  47.         var thirdNumber = Regex.Match(dataLine.Value.Substring(startPosition), @"[0-9]+(\.)[0-9]+\s+?");
  48.         startPosition += thirdNumber.Index + thirdNumber.Length;
  49.         var nameLastPart = Regex.Match(dataLine.Value.Substring(startPosition), @"([a-záéíóöőúüű]+\s)+");
  50.         var name = nameFirstPart.Value + nameLastPart.Value;
  51.  
  52.         var tableSeparator = @";";
  53.         File.AppendAllText(destinationFilePath,
  54.                            (identifier.Value
  55.                             + tableSeparator
  56.                             + name
  57.                             + tableSeparator
  58.                             + firstNumber.Value
  59.                             + tableSeparator
  60.                             + secondNumber.Value
  61.                             + tableSeparator
  62.                             + thirdNumber.Value
  63.                             + Environment.NewLine),
  64.                            Encoding.UTF8);
  65.  
  66.         dataLine = dataLine.NextMatch();
  67.       }
  68.  
  69.       Console.WriteLine("Table conversion has been finished.");
  70.       Console.ReadKey();
  71.     }
  72.   }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement