regex string patterns c# code example

Example 1: regex in c#

string regex = @"\me\";
steing testRegex = "Hit me with that titan Boa baby.";

Regex re = new Regex(regex);


if (re.IsMatch(testRegex)){
 // WIll return true if it matches. # It does
  return true;
} else {
 // Will return false if it does not macth 
  return false;
  
}

Example 2: c# regex

public static Dictionary<string, string> FindPattern(this string text, string pattern)
{
    Regex expression = new Regex(pattern);
    Match match = expression.Match(text);
    Dictionary<string, string> matchvalue = new Dictionary<string, string>();
    if (match.Success)
    {
        foreach (string g in expression.GetGroupNames())
        {
            matchvalue[g] = match.Groups[g].Value;
        }
        return matchvalue;
    }
    else
    {
        foreach (string g in expression.GetGroupNames())
        {
            matchvalue[g] = "";
        }
    }
    return matchvalue;
}