Parse String to Dictionary<String, String>

S

shapper

Hello,

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.

Thanks,

Miguel
 
A

Arne Vajhøj

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();
}
}
}

Arne
 
S

shapper

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;

   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();
         }
     }

}

Arne

Hello Arne,

Just a question:

Sometimes the Connection String in App.Config contains the &quot;
instead of '.

And when I load it in my C# code the &quot; 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?

Thanks,
Miguel
 
A

Arne Vajhøj

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:


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'

Key = "Provider Connection String"
Value = "Data Source=localhost; Initial
Catalog=AdventureWorks;Integrated Security=True;Connection Timeout=60"
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&quot;
instead of '.

And when I load it in my C# code the&quot; 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*(([^'"";]+)|('[^']*')|(""[^']*""))"

Arne
 
S

shapper

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&quot;
instead of '.
And when I load it in my C# code the&quot; 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*(([^'"";]+)|('[^']*')|(""[^']*""))"

Arne

Hello,

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?

Thank You,
Miguel
 
S

shapper

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&quot;
instead of '.
And when I load it in my C# code the&quot; 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*(([^'"";]+)|('[^']*')|(""[^']*""))"

Arne

Hello,

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?

Thank You,
Miguel

Please, anyone?
 
A

Arne Vajhøj

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:


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'

Key = "Provider Connection String"
Value = "Data Source=localhost; Initial
Catalog=AdventureWorks;Integrated Security=True;Connection Timeout=60"
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&quot;
instead of '.
And when I load it in my C# code the&quot; 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.

Try:

@"\s*([^=;]+?)\s*=\s*(([^'"";]+)|('[^']*')|((?:"")[^']*(?:"")))"

Arne
 
A

Arne Vajhøj

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&quot;
instead of '.

And when I load it in my C# code the&quot; 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.

Try:

@"\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!
 
S

shapper

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&quot;
instead of '.
And when I load it in my C# code the&quot; 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!

Arne I didn't ask specifically the use of Regex. :)

I just followed your first example that used Regex.

In fact when I posted this thread I was trying Subtring and Split but
I was not able to make it work.
 
A

Arne Vajhøj

On 12-10-2010 08:04, shapper wrote:
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:


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'

Key = "Provider Connection String"
Value = "Data Source=localhost; Initial
Catalog=AdventureWorks;Integrated Security=True;Connection
Timeout=60"
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&quot;
instead of '.
And when I load it in my C# code the&quot; 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();
}
}

}
PS: But I still think Substring is a good solution!

Arne I didn't ask specifically the use of Regex. :)

I just followed your first example that used Regex.

In fact when I posted this thread I was trying Subtring and Split but
I was not able to make it work.

I would use Regex to get the "xxxx" string.

But then I would use Substring to go from "xxxx" to xxxx.

Cleaner code.

And that Substring is easy to get working!

Arne
 
S

shapper

On 12-10-2010 22:20, Arne Vajhøj wrote:
On 12-10-2010 08:04, shapper wrote:
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&quot;
instead of '.
And when I load it in my C# code the&quot; 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.
Try:
@"\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();
          }
      }
}
PS: But I still think Substring is a good solution!
Arne I didn't ask specifically the use of Regex. :)
I just followed your first example that used Regex.
In fact when I posted this thread I was trying Subtring and Split but
I was not able to make it work.

I would use Regex to get the "xxxx" string.

But then I would use Substring to go from "xxxx" to xxxx.

Cleaner code.

And that Substring is easy to get working!

Arne

Arne,

I got it ... It is working now.

Thank You Very Much,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top