Advertisement
dimipan80

Oh, My Girl!

May 28th, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. namespace Oh_My_Girl
  2. {
  3.     using System;
  4.     using System.Text;
  5.     using System.Text.RegularExpressions;
  6.  
  7.     class OhMyGirl
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string keyPattern = GetKeyPattern();
  12.  
  13.             StringBuilder text = new StringBuilder();
  14.             string inputLine = Console.ReadLine();
  15.             while (inputLine != "END")
  16.             {
  17.                 text.Append(inputLine);
  18.                 inputLine = Console.ReadLine();
  19.             }
  20.  
  21.             StringBuilder result = new StringBuilder();
  22.             MatchCollection matches = Regex.Matches(text.ToString(), keyPattern);
  23.             foreach (Match match in matches)
  24.             {
  25.                 result.Append(match.Groups[1].Value);
  26.             }
  27.  
  28.             Console.WriteLine(result);
  29.         }
  30.  
  31.         private static string GetKeyPattern()
  32.         {
  33.             string key = Console.ReadLine();
  34.             StringBuilder sb = new StringBuilder();
  35.             if (!char.IsLetterOrDigit(key[0]) && !char.IsWhiteSpace(key[0]))
  36.             {
  37.                 sb.Append('\\');
  38.             }
  39.  
  40.             sb.Append(key[0]);
  41.  
  42.             for (int i = 1; i < key.Length - 1; i++)
  43.             {
  44.                 if (char.IsDigit(key[i]))
  45.                 {
  46.                     sb.Append("[0-9]*");
  47.                 }
  48.                 else if (char.IsUpper(key[i]))
  49.                 {
  50.                     sb.Append("[A-Z]*");
  51.                 }
  52.                 else if (char.IsLower(key[i]))
  53.                 {
  54.                     sb.Append("[a-z]*");
  55.                 }
  56.                 else if (char.IsWhiteSpace(key[i]))
  57.                 {
  58.                     sb.Append("\\s");
  59.                 }
  60.                 else
  61.                 {
  62.                     sb.Append('\\').Append(key[i]);
  63.                 }
  64.             }
  65.  
  66.             if (!char.IsLetterOrDigit(key[key.Length - 1]) &&
  67.                 !char.IsWhiteSpace(key[key.Length - 1]))
  68.             {
  69.                 sb.Append('\\');
  70.             }
  71.  
  72.             sb.Append(key[key.Length - 1]);
  73.  
  74.             return (sb + "(.{2,6})" + sb);
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement