Advertisement
svephoto

MessageEncrypter [C#]

Jul 31st, 2020 (edited)
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. namespace MessageEncrypter
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int number = int.Parse(Console.ReadLine());
  11.  
  12.             for (int i = 0; i < number; i++)
  13.             {
  14.                 string input = Console.ReadLine();
  15.  
  16.                 string firstPattern = @"[@](?<tag>[A-Z][a-z]{2,})[@]: \[(?<firstNumber>[A-Za-z])\]\|\[(?<secondNumber>[A-Za-z])]\|\[(?<thirdNumber>[A-Za-z])\]\|$";
  17.                 string secondPattern = @"[*](?<tag>[A-Z][a-z]{2,})[*]: \[(?<firstNumber>[A-Za-z])\]\|\[(?<secondNumber>[A-Za-z])]\|\[(?<thirdNumber>[A-Za-z])\]\|$";
  18.  
  19.                 var firstRegex = new Regex(firstPattern);
  20.                 var secondRegex = new Regex(secondPattern);
  21.                 var firstMatch = firstRegex.Match(input);
  22.                 var secondMatch = secondRegex.Match(input);
  23.  
  24.                 if (firstMatch.Success)
  25.                 {
  26.                     string tag = firstMatch.Groups["tag"].Value;
  27.                     char firstNumber = char.Parse(firstMatch.Groups["firstNumber"].Value);
  28.                     char secondNumber = char.Parse(firstMatch.Groups["secondNumber"].Value);
  29.                     char thirdNumber = char.Parse(firstMatch.Groups["thirdNumber"].Value);
  30.                     Console.WriteLine($"{tag}: {(int)firstNumber} {(int)secondNumber} {(int)thirdNumber} ");
  31.                 } else if (secondMatch.Success)
  32.                 {
  33.                     string tag = secondMatch.Groups["tag"].Value;
  34.                     char firstNumber = char.Parse(secondMatch.Groups["firstNumber"].Value);
  35.                     char secondNumber = char.Parse(secondMatch.Groups["secondNumber"].Value);
  36.                     char thirdNumber = char.Parse(secondMatch.Groups["thirdNumber"].Value);
  37.                     Console.WriteLine($"{tag}: {(int)firstNumber} {(int)secondNumber} {(int)thirdNumber} ");
  38.                 }
  39.                 else
  40.                 {
  41.                     Console.WriteLine("Valid message not found!");
  42.                     continue;
  43.                 }
  44.             }
  45.         }
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement