28012

Question:
I have a string
<blockquote>"FirstName=John, LastName=Smith, Address=1 Wall Street, NY"
</blockquote>which needs to be split into a dictionary as:
<ul><li>{"FirstName", "John"} </li> <li>{"LastName", "Smith"} </li> <li>{"Address", "1 Wall Street, NY"}</li> </ul>How can this be achieved using Regex
considering Address field value has the delimiter ','
in it?
Also lets say I have the following string (note the colon in Address):
<blockquote>"FirstName=John, LastName=Smith, Address:1 Wall Street, NY"
</blockquote>How can the above mentioned result be with either = or :
acting as the key value pair separator?
Assuming keys cannot contain any of delimeters (comma should be followed by key to separate pairs)
var data = "FirstName=John, LastName=Smith, Address:1 Wall Street, NY, USA, TestKey=TestValue";
var dic = new Dictionary<string, string>();
var reg = @"([^=:,]*)[=:](.*?)(?:$|,\s*(?=[^=:,]*[=:]))";
foreach (Match m in Regex.Matches(data, reg)) {
var key = m.Groups[1].Value;
var val = m.Groups[2].Value;
dic[key] = val;
Console.WriteLine("{0} = {1}", key, val);
}