Advertisement
knoteva

Regex_mixed_groups_test

Jul 23rd, 2019
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.51 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. namespace _07._Vending_Machine
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             var regex = new Regex(@"\b(?<month>[A-Z][a-z]{2})([-.\/])(?<day>\d{2})\1(?<year>\d{4})_([a-z]+)_(?<someOtherGroup>[0-9]+)_([0-9]+)+\b");
  11.  
  12.  
  13.             string dates = "Apr.16.2017_jan_07_2019"; //Console.ReadLine();
  14.  
  15.             var matches = regex.Matches(dates);
  16.  
  17.             foreach (Match item in matches)
  18.             {
  19.                 var separator = item.Groups[1].Value; // = ([-.\/])// result: .
  20.                 var secondMonth = item.Groups[2].Value; // = ([a-z]+)// result: jan
  21.                 var secondYear = item.Groups[3].Value; // = ([-.\/])// result: 2019
  22.                 var month = item.Groups[4].Value; // = item.Groups["month"].Value;// result: Apr
  23.                 var day = item.Groups[5].Value; // = item.Groups["day"].Value; // result: 16              
  24.                 var year = item.Groups[6].Value; // = item.Groups["year"].Value; // result: 2017
  25.                 var someOtherGroup = item.Groups[7].Value; // = item.Groups["someOtherGroup"].Value; // result: 07
  26.  
  27.                 Console.WriteLine(separator);
  28.                 Console.WriteLine(secondMonth);
  29.                 Console.WriteLine(secondYear);
  30.                 Console.WriteLine(month);
  31.                 Console.WriteLine(day);
  32.                 Console.WriteLine(year);
  33.                 Console.WriteLine(someOtherGroup);
  34.             }
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement