IPetrov007

CubicWithRegex

Apr 23rd, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _04_CubicwithRegex
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. var pattrern = @"(\d+)([a-zA-Z]+)(\W|[0-9]+)";
  15. var regex = new Regex(pattrern);
  16.  
  17. while (true)
  18. {
  19. var input = Console.ReadLine();
  20. if (input == "Over!")
  21. {
  22. break;
  23. }
  24. var number = int.Parse(Console.ReadLine());
  25.  
  26. var matches = regex.Match(input);
  27.  
  28. if (matches.Length != input.Length)
  29. {
  30. continue;
  31. }
  32.  
  33. var firstMatch = matches.Groups[1].Value;
  34. var text = matches.Groups[2].Value;
  35. var lastMatch = matches.Groups[3].Value;
  36.  
  37. var digits = "";
  38. foreach (var character in firstMatch)
  39. {
  40. var num = int.Parse(character.ToString());
  41. digits += num;
  42. }
  43. foreach (var character in lastMatch)
  44. {
  45. var num = -1;
  46. if (int.TryParse(character.ToString(), out num))
  47. {
  48. digits += num;
  49. }
  50. }
  51. var verificationCode = "";
  52. foreach (var index in digits)
  53. {
  54. var digit = int.Parse(index.ToString());
  55. if (digit < text.Length)
  56. {
  57. verificationCode += text[digit];
  58. }
  59. else
  60. {
  61. verificationCode += " ";
  62. }
  63. }
  64. Console.WriteLine($"{text} == {verificationCode}");
  65. }
  66. }
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment