Advertisement
dragonbs

Fancy Barcodes

Mar 27th, 2023
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         string regex1 = @"\A\@#+[A-Z]([A-Za-z\d]{4,})[A-Z]\@#+\Z";
  9.         string regex2 = @"\d";
  10.         int numberOfProducts = int.Parse(Console.ReadLine());
  11.         for (int i = 0; i < numberOfProducts; i++)
  12.         {
  13.             string entry = Console.ReadLine();
  14.             Match match = Regex.Match(entry, regex1);
  15.             if (match.Success)
  16.             {
  17.                 string rawData = match.Groups[1].Value;
  18.                 MatchCollection digits = Regex.Matches(rawData, regex2);
  19.                 string productGroup = string.Empty;
  20.                 if (digits.Count > 0)
  21.                 {
  22.                     foreach (Match digit in digits)
  23.                         productGroup += digit;
  24.                 }
  25.                 else
  26.                 {
  27.                     productGroup = "00";
  28.                 }
  29.                 Console.WriteLine($"Product group: {productGroup}");
  30.             }
  31.             else
  32.                 Console.WriteLine("Invalid barcode");
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement