bullit3189

Key Replacer - Regex Archive

Feb 26th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. namespace _05KeyReplacer
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. string key = Console.ReadLine();
  11. string text = Console.ReadLine();
  12.  
  13. string start = string.Empty;
  14.  
  15. for (int i = 0; i < key.Length; i++)
  16. {
  17. char curr = key[i];
  18.  
  19. if (curr == '|' || curr == '\\' || curr == '<')
  20. {
  21. break;
  22. }
  23. else if (char.IsLetter(curr))
  24. {
  25. start += curr;
  26. }
  27. }
  28.  
  29. string reversedEnd = string.Empty;
  30.  
  31. for (int i = key.Length - 1; i >= 0; i--)
  32. {
  33. char curr = key[i];
  34.  
  35. if (curr == '|' || curr == '\\' || curr == '<')
  36. {
  37. break;
  38. }
  39. else if (char.IsLetter(curr))
  40. {
  41. reversedEnd += curr;
  42. }
  43. }
  44.  
  45. string end = string.Empty;
  46.  
  47. for (int i = reversedEnd.Length - 1; i >= 0; i--)
  48. {
  49. char curr = reversedEnd[i];
  50.  
  51. end += curr;
  52. }
  53.  
  54. string pattern = $@"({start}(?<word>.{{0,}}?){end})";
  55.  
  56. string result = string.Empty;
  57.  
  58. MatchCollection matches = Regex.Matches(text, pattern);
  59.  
  60. foreach (Match match in matches)
  61. {
  62. string word = match.Groups["word"].Value;
  63.  
  64. result += word;
  65. }
  66.  
  67. if (result==string.Empty)
  68. {
  69. Console.WriteLine("Empty result");
  70. return;
  71. }
  72. Console.WriteLine(result);
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment