Advertisement
AlexVanchov

2

Apr 4th, 2020
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. namespace _2
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Regex regex = new Regex(@"@#+([A-Z]{1}[a-z,A-Z,0-9]{4,}[A-Z]{1})@#+");
  11.             int times = int.Parse(Console.ReadLine());
  12.  
  13.             for (int i = 0; i < times; i++)
  14.             {
  15.                 var inputBarcode = Console.ReadLine();
  16.                 Match match = regex.Match(inputBarcode);
  17.  
  18.                 if (match.Success)
  19.                 {
  20.                     var validBarcode = match.Groups[1].Value;
  21.                     var groupCode = GetBarcodeGroup(validBarcode);
  22.                     Console.WriteLine($"Product group: {groupCode}");
  23.                 }
  24.                 else
  25.                 {
  26.                     Console.WriteLine("Invalid barcode");
  27.                 }
  28.             }
  29.         }
  30.  
  31.         public static string GetBarcodeGroup(string barcode)
  32.         {
  33.             var groupCode = string.Empty;
  34.  
  35.             foreach (var letter in barcode)
  36.             {
  37.                 if (char.IsNumber(letter))
  38.                     groupCode += letter;
  39.             }
  40.  
  41.             if (groupCode == "")
  42.                 groupCode = "00";
  43.  
  44.             return groupCode;
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement