Advertisement
jyoung12387

Match Collection Basic

Mar 14th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace MarchFourteen
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string pattern = @"\d";
  15.             Regex regex = new Regex(pattern);
  16.  
  17.             string text = "Hi there, my number is 12314";
  18.  
  19.             MatchCollection matchCollection = regex.Matches(text);
  20.  
  21.             Console.WriteLine($"{matchCollection.Count} hits found:\n{text}");
  22.  
  23.             foreach(Match hit in matchCollection)
  24.             {
  25.                 GroupCollection group = hit.Groups;
  26.                 Console.WriteLine($"{group[0].Value} found at {group[0].Index}");
  27.             }
  28.  
  29.             Console.ReadLine();
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement