fork download
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Collections.Generic;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. Dictionary<string, string> configuracoes = new Dictionary<string, string>();
  10.  
  11. string re = "([^\\s].*?)=([^;]+?)(;|$)";
  12.  
  13. string input = "Data Source=.\\SQLEXPRESS;Initial Catalog=Target_Database;User Id=User; Password=MyPassword";
  14.  
  15. foreach (Match m in Regex.Matches(input, re)) {
  16.  
  17. //Exibe os resultados encontrados, pode remover isto é só para testes
  18. //Console.WriteLine("Chave: {0} - Valor: {1}", m.Groups[1].Value, m.Groups[2].Value);
  19.  
  20. //Salva o item
  21. configuracoes[m.Groups[1].Value] = m.Groups[2].Value;
  22. }
  23.  
  24. Console.WriteLine("Valor: {0}", configuracoes["Data Source"]); // Retorna .\SQLEXPRESS
  25. Console.WriteLine("Valor: {0}", configuracoes["Initial Catalog"]); // Retorna Target_Database
  26. Console.WriteLine("Valor: {0}", configuracoes["User Id"]); // Retorna User
  27. Console.WriteLine("Valor: {0}", configuracoes["Password"]); // Retorna MyPassword
  28. }
  29. }
Success #stdin #stdout 0.04s 134720KB
stdin
Standard input is empty
stdout
Valor: .\SQLEXPRESS
Valor: Target_Database
Valor: User
Valor: MyPassword