using System.Text.RegularExpressions;
public static void Main()
Console.WriteLine("Postion\n");
Regex reg = new Regex("the");
string str1 = "the quick brown fox jumped over the lazy dog";
MatchCollection matchSet;
matchSet = reg.Matches(str1);
foreach (Match aMatch in matchSet)
Console.WriteLine("found a match at: " + aMatch.Index);
Console.WriteLine("\n==================\n");
Console.WriteLine("QUANTIFIERS:\n");
Console.WriteLine("+\n");
string[] words = new string[] {"bad", "boy", "baaad","bear", "bend","baad"};
foreach (string word in words)
if (Regex.IsMatch(word, "ba+"))
Console.WriteLine("\n{n}\n");
foreach (string word in words)
if (Regex.IsMatch(word, "ba{2}d"))
foreach (string word in words)
if (Regex.IsMatch(word, "ba{1,3}d"))
Console.WriteLine("\n==================\n");
Console.WriteLine("Match word, we want to get <b> only \n");
string[] wordss = new string[]{"Part", "of", "this","<b>string</b>", "is", "bold"};
foreach (string word in wordss) {
if (Regex.IsMatch(word, regExp)) {
bMatch = Regex.Matches(word, regExp);
for(int i = 0; i < bMatch.Count; i++)
Console.WriteLine(bMatch[i].Value);}}
Console.WriteLine("\nfixed\n");
foreach (string word in wordss) {
if (Regex.IsMatch(word, regExp)) {
bMatch = Regex.Matches(word, regExp);
for(int i = 0; i < bMatch.Count; i++)
Console.WriteLine(bMatch[i].Value);}}
Console.WriteLine("\n==================\n");
Console.WriteLine("Match postiong:\n");
string str2 = "the quick brown fox jumped over thelazy dog";
matchSet = Regex.Matches(str2, ".");
foreach (Match cMatch in matchSet)
Console.WriteLine("matches at: " + cMatch.Index);
Console.WriteLine("\n==================\n");
Console.WriteLine("Match postiong with rule:\n");
str2 = "THE quick BROWN fox JUMPED over THE lazy DOG";
matchSet = Regex.Matches(str2, "[a-z]");
foreach (Match cMatch in matchSet)
Console.WriteLine("Matches at: " + cMatch.Index);
Console.WriteLine("\n==================\n");
words = new string[]{"heal", "heel","noah", "techno"};
Console.WriteLine("start with h\n");
foreach (string word in words){
if (Regex.IsMatch(word, regExp)) {
dMatch = Regex.Match(word, regExp);
Console.WriteLine("Matched: " + word + " at position: " + dMatch.Index);}}
Console.WriteLine("\nEnd with h\n");
foreach (string word in words){
if (Regex.IsMatch(word, regExp)) {
dMatch = Regex.Match(word, regExp);
Console.WriteLine("Matched: " + word + " at position: " + dMatch.Index);}}
Console.WriteLine("\n==================\n");
Console.WriteLine("Grouping\n");
string words1 = "08/14/57 46 02/25/59 45 06/05/85 18" +"03/12/88 16 09/09/90 13";
string regExp1 = "(\\s\\d{2}\\s)";
matchSet = Regex.Matches(words1,regExp1);
foreach (Match aMatch in matchSet)
Console.WriteLine(aMatch.Groups[0].Captures[0]);
Console.WriteLine("\nNamed Grouping\n");
regExp1 = "(?<dates>(\\d{2}/\\d{2}/\\d{2}))\\s";
matchSet = Regex.Matches(words1,regExp1);
foreach (Match aMatch in matchSet)
Console.WriteLine("Date: {0}", aMatch.Groups["dates"]);
Console.WriteLine("\n==================");
string dates = "08/14/57 46 02/25/59 45 06/05/85 18 " + "03/12/88 16 09/09/90 13";
regExp ="(?<dates>(\\d{2}/\\d{2}/\\d{2}))\\s(?<ages>(\\d{2}))\\s";
matchSet = Regex.Matches(dates, regExp);
foreach (Match aMatch in matchSet) {
foreach (Capture aCapture in aMatch.Groups["dates"].Captures)
Console.WriteLine("date capture: " +aCapture.ToString());
foreach (Capture aCapture in aMatch.Groups["ages"].Captures)
Console.WriteLine("age capture: " +aCapture.ToString());}