using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
Dictionary<string, string> configuracoes = new Dictionary<string, string>();
string re = "([^\\s].*?)=([^;]+?)(;|$)";
string input = "Data Source=.\\SQLEXPRESS;Initial Catalog=Target_Database;User Id=User; Password=MyPassword";
foreach (Match m in Regex.Matches(input, re)) {
//Exibe os resultados encontrados, pode remover isto é só para testes
//Console.WriteLine("Chave: {0} - Valor: {1}", m.Groups[1].Value, m.Groups[2].Value);
//Salva o item
configuracoes[m.Groups[1].Value] = m.Groups[2].Value;
}
Console.WriteLine("Valor: {0}", configuracoes["Data Source"]); // Retorna .\SQLEXPRESS
Console.WriteLine("Valor: {0}", configuracoes["Initial Catalog"]); // Retorna Target_Database
Console.WriteLine("Valor: {0}", configuracoes["User Id"]); // Retorna User
Console.WriteLine("Valor: {0}", configuracoes["Password"]); // Retorna MyPassword
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uVGV4dC5SZWd1bGFyRXhwcmVzc2lvbnM7CnVzaW5nIFN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljOwoKcHVibGljIGNsYXNzIFRlc3QKewogICAgcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKQogICAgewogICAgICAgIERpY3Rpb25hcnk8c3RyaW5nLCBzdHJpbmc+IGNvbmZpZ3VyYWNvZXMgPSBuZXcgRGljdGlvbmFyeTxzdHJpbmcsIHN0cmluZz4oKTsKCiAgICAgICAgc3RyaW5nIHJlID0gIihbXlxcc10uKj8pPShbXjtdKz8pKDt8JCkiOwoKICAgICAgICBzdHJpbmcgaW5wdXQgPSAiRGF0YSBTb3VyY2U9LlxcU1FMRVhQUkVTUztJbml0aWFsIENhdGFsb2c9VGFyZ2V0X0RhdGFiYXNlO1VzZXIgSWQ9VXNlcjsgUGFzc3dvcmQ9TXlQYXNzd29yZCI7CgogICAgICAgIGZvcmVhY2ggKE1hdGNoIG0gaW4gUmVnZXguTWF0Y2hlcyhpbnB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