c# Regex.Match code example

Example 1: c# regex match

const string input = "Lorem ipsum dolor sit %download%#456 amet, consectetur adipiscing %download%#3434 elit. Duis non nunc nec mauris feugiat porttitor. Sed tincidunt blandit dui a viverra%download%#298. Aenean dapibus nisl %download%#893434 id nibh auctor vel tempor velit blandit.";

static void Main(string[] args)
{
    Regex expression = new Regex(@"%download%#(?<Identifier>[0-9]*)");
    var results = expression.Matches(input);
    foreach (Match match in results)
    {
        Console.WriteLine(match.Groups["Identifier"].Value);
    }
}

Example 2: 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 3: c# regex get matched string

Regex filter = new Regex(@"(\d+)");

foreach (var item in checkedListBox1.CheckedItems)
{
    var match = filter.Match(item.ToString());
    if (match.Success)
    {
        MessageBox.Show(match.Value);
    }    
}

Example 4: c# regex match

var regex = new Regex(@"(?<=%download%#)\d+");
return regex.Matches(strInput);

Example 5: 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;
}