ShP ShP - 28 days ago 12
C# Question

Parsing into dictionary with regex as separator for splitting

As I said in title, I think the idea would be to split it by something like this

\d+?=.*?\d=
but not quite sure... Any idea how best to parse this string:

1=Some dummy sentence
2=Some other sentence 3=Third sentence which can be in the same line
4=Forth sentence
some text which shouldn't be captured and spplitted


And what I'm hoping to get from this is a Dictionary which will have this number for key, and this string in the value, so for example:

1, "Some dummy sentence"
2, "Some other sentence"
3, "Third sentence which can be in the same line"
4, "Forth sentence"

Answer

Method to parse text into dictionary:

public static Dictionary<int, string> GetValuesToDictionary(string text)
{
    var pattern = @"\b(\d+)=([^\d\n]+)";
    var regex = new Regex(pattern);

    var pairs = new Dictionary<int, string>();
    var matches = regex.Matches(text);
    foreach (Match match in matches)
    {
        var key = int.Parse(match.Groups[1].Value);
        var value = match.Groups[2].Value;
        if (!pairs.ContainsKey(key))
        {
            pairs.Add(key, value);
        }
        //pairs.Add(key, value);
    }

    return pairs;
}

In this case i check if lkey already exists and if so i do not add it but you can see for yourself if you need this check.