petaryankov00

FancyBarcodes

Dec 5th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4.  
  5. namespace FancyBarcodes
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int n = int.Parse(Console.ReadLine());
  12.             string pattern = @"(@\#+)([A-Z]([A-Za-z0-9]){4,}[A-Z])\1";
  13.             string digitPattern = @"\d+";
  14.             Regex regex = new Regex(pattern);
  15.             for (int i = 0; i < n; i++)
  16.             {
  17.                 string barcode = Console.ReadLine();
  18.                 Match match = regex.Match(barcode);
  19.                 if (!match.Success)
  20.                 {
  21.                     Console.WriteLine("Invalid barcode");
  22.                 }
  23.                 else
  24.                 {
  25.                     Regex digitRegex = new Regex(digitPattern);
  26.                     string newBarcode = match.ToString();
  27.                     MatchCollection digits = digitRegex.Matches(newBarcode);
  28.                     if (digits.Count == 0)
  29.                     {
  30.                         Console.WriteLine($"Product group: 00");
  31.                     }
  32.                     else
  33.                     {
  34.                         StringBuilder sb = new StringBuilder();
  35.                         foreach (var digit in digits)
  36.                         {
  37.                             sb.Append(digit);
  38.                         }
  39.                         string productGroup = sb.ToString();
  40.                         Console.WriteLine($"Product group: {productGroup}");
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.     }
  46. }
  47.  
Add Comment
Please, Sign In to add comment