Advertisement
elena1234

MatchDates*-regex

Nov 20th, 2020 (edited)
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. namespace text
  5. {
  6.     class MainClass
  7.     {
  8.         public static void Main(string[] args)
  9.         {
  10.             var regex = new Regex(@"\b(?<day>[0-9]{2})([-.\/])(?<month>[A-Z][a-z]{2})\1(?<year>[0-9]{4})\b");//group 1 is the first group without name; group 2 is the second group without name; all groups with names are after tha last group without name;
  11.             var text = Console.ReadLine();
  12.             var matches = regex.Matches(text);
  13.  
  14.             foreach (Match match in matches)
  15.             {
  16.                 var day = match.Groups["day"].Value;//group 2
  17.                 var month = match.Groups["month"].Value;//group 3
  18.                 var year = match.Groups["year"].Value;//group 4
  19.                 Console.WriteLine($"Day: {day}, Month: {month}, Year: {year}");
  20.             }
  21.         }
  22.     }
  23. }    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement