On 11-10-2010 14:18, shapper wrote:
On 09-10-2010 21:56, shapper wrote:
I have the following string:
String conn = " Provider=System.Data.SqlClient;
Metadata=c:\metadata|c:\Metadata\Sql;
Provider Connection String= 'Data Source=localhost; Initial
Catalog=AdventureWorks;Integrated Security=True;Connection
Timeout=60'
";
This string contains pairs of Keys / Values. For example:
Provider=System.Data.SqlClient;
Becomes
Key = "Provider", Value = "System.Data.SqlClient".
";" separate each Key/Value pair.
If a Value contains '' I want to remove those. For example:
2) Provider Connection String= 'Data Source=localhost; Initial
Catalog=AdventureWorks;Integrated Security=True;Connection
Timeout=60'
Becomes
Key = "Provider Connection String"
Value = "Data Source=localhost; Initial
Catalog=AdventureWorks;Integrated Security=True;Connection
Timeout=60"
AND NOT:
Value = "'Data Source=localhost; Initial
Catalog=AdventureWorks;Integrated Security=True;Connection
Timeout=60'"
How can I parse such strings to a Dictionary<String, String>?
I am not sure if the string contains only 3 Key/Value pairs. I just
know that is follows this rule.
Some quick regex:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace E
{
public class Program
{
private static readonly Regex re = new
Regex(@"\s*([^=;]+?)\s*=\s*(([^';]+)|('[^']*'))",
RegexOptions.Compiled);
public static Dictionary<string, string> SpecialParse(string s)
{
Dictionary<string, string> res = new Dictionary<string,
string>();
foreach(Match m in re.Matches(s))
{
res.Add(m.Groups[1].Value, m.Groups[2].Value);
}
return res;
}
public static void Main(string[] args)
{
string s = @"Provider=System.Data.SqlClient;
Metadata=c:\metadata|c:\Metadata\Sql; Provider Connection String='Data
Source=localhost; Initial Catalog=AdventureWorks;Integrated
Security=True;Connection Timeout=60'";
Dictionary<string, string> d = SpecialParse(s);
foreach(KeyValuePair<string, string> kvp in d)
{
Console.WriteLine(kvp.Key + " : " + kvp.Value);
}
Console.ReadKey();
}
}
}
Just a question:
Sometimes the Connection String in App.Config contains the"
instead of '.
And when I load it in my C# code the" is replaced by \"
I am having some trouble including that on the Regex you created
because the \" always messes up the Regex string.
Could you tell me how to solve this?
Untested:
@"\s*([^=;]+?)\s*=\s*(([^'"";]+)|('[^']*')|(""[^']*""))"
Still having a few problems. Sorry, I am not familiar with Regex.
This is the String I get from the App.Config:
String connection = "metadata=res://*/Context.csdl|res://*/
Context.ssdl|res://*/
Context.msl;provider=System.Data.SqlClient;provider connection string=
\"Data Source=FLYONDREAMS\\SQLEXPRESS;Initial
Catalog=FlyOnPages;Integrated
Security=True;MultipleActiveResultSets=True\""
And when I use the Regex I get the following for the provider
connectionstring:
"\"Data Source=FLYONDREAMS\\SQLEXPRESS;Initial
Catalog=FlyOnPages;Integrated
Security=True;MultipleActiveResultSets=True\""
So it seems I am getting two " on start and end when I should get only
one. I should remove the \".
Can I solve this problem in the Regex?
It would not be that hard to remove those "" with Substring!
But it should also be possible with Regex.
@"\s*([^=;]+?)\s*=\s*(([^'"";]+)|('[^']*')|((?:"")[^']*(?:"")))"
Hm.
That does not work.
But then we can take the brutal methods in use:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace E
{
public class Program
{
private static readonly Regex re = new
Regex(@"\s*([^=;]+?)\s*=\s*(([^'"";]+)|('([^'])*')|(""([^']*)""))" ,
RegexOptions.Compiled);
public static Dictionary<string, string> SpecialParse(string s)
{
Dictionary<string, string> res = new Dictionary<string,
string>();
foreach(Match m in re.Matches(s))
{
res.Add(m.Groups[1].Value, m.Groups[7]..Value != "" ?
m.Groups[7].Value : (m.Groups[5].Value != "" ? m.Groups[5].Value :
m.Groups[2].Value));
}
return res;
}
public static void Main(string[] args)
{
string s = @"Provider=System.Data.SqlClient;
Metadata=c:\metadata|c:\Metadata\Sql; Provider Connection String=""Data
Source=localhost; Initial Catalog=AdventureWorks;Integrated
Security=True;Connection Timeout=60""";
Dictionary<string, string> d = SpecialParse(s);
foreach(KeyValuePair<string, string> kvp in d)
{
Console.WriteLine(kvp.Key + " : " + kvp.Value);
}
Console.ReadKey();
}
}
}
Arne
PS: But I still think Substring is a good solution!