c# regex string pattern code example

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