c# regex get capture value code example

Example 1: c# regex get capture value

public static List>> FindMultiMatchPatterns(this string text, string pattern)
        {
            List>> matchvalues = new List>>();

            Regex expression = new Regex(pattern);
            Match match = expression.Match(text);
            while (match.Success)
            {
                Dictionary> matchvalue = new Dictionary>();
                foreach (string g in expression.GetGroupNames())
                {
                    matchvalue[g] = new List();
                    foreach (var c in match.Groups[g].Captures)
                    {
                        matchvalue[g].Add(c.ToString());
                    }
                }
                matchvalues.Add(matchvalue);
                match = match.NextMatch();
            }
            return matchvalues;
        }

Example 2: c# get regedit value

try
{
  	string reqURL = "Software\\Wow6432Node\\MySQL AB\\MySQL Connector\\Net";
    RegistryKey key = Registry.LocalMachine.OpenSubKey(reqUrl);
    if (key != null)
    {
        Object o = key.GetValue("Version");
        if (o != null)
        {
            Version version = new Version(o as String);  
			//"as" because it's REG_SZ...otherwise ToString() might be safe(r)
            
			Version broken = new Version("6.7.4");
          
          	//This is where the error is occuring
            if (version.Equals.(broken)) 
            {
              	var section = ConfigurationManager.GetSection("system.data"); 
                DataSet dataSet = section as DataSet;

                DataView vi = dataSet.Tables[0].DefaultView;
                vi.Sort = "Name";
                if (vi.Find("MySql") == -1)
                {
                    dataSet.Tables[0].Rows.Add(
						"MySql",
						"MySql.Data.MySqlClient",
						"MySql.Data.MySqlClient",
                        typeof(MySqlClientFactory).AssemblyQualifiedName
                    );
                }

            }

        }
    }
}
catch (Exception ex)
{
  	//just for demonstration...it's always best to handle specific exceptions
     //react appropriately
}

Tags:

Misc Example