Regular Expressions - RegEx


Intro

My notes on C#'s support for regular expressions via the System.Text.RegularExpressions namespace.

 


Define The Pattern To Match

 

You first need to define the RegEx pattern to be used. You can define the regex pattern as a string.

string pattern = @"^dog.*"; Regex rx = new Regex(pattern, RegexOptions.IgnoreCase);

 


Execute Match

 

DotNetFiddle: https://dotnetfiddle.net/OqEBzt

string input1 = "dog runs around\ndog2 runs around too."; Match match = rx.Match(input1); while (match.Success) { Console.WriteLine("'{0}' found at position {1}.", match.Value, match.Index); match = match.NextMatch(); }