Question

I'm sure this isn't as complicated as I'm making it. I have a string that follows the following pattern:

#,"value"#,"next value"#,"next value" . . .

I need to parse out the number/value pairs in order to use the data in my application.

Here is a sample of a return:

0,"120"1,""10,"298630427"29,"577015971830"30,"SG MSNA "33,"A1"34,"4625"35,"239"36,"2105"37,"2759"60,"15"112,"0"

To complicate matters the string can contain newline characters (\r,\n, or \r\n). I can handle that by simply removing the newlines with a few string.replace calls.

I would ultimately like to parse the data into key/value pairs. I just can't seem to get my mind unto the right path.

I apologize if this is trivial but I've been pulling 18+ hours days for two months trying to meet a deadline and my brain is shot. Any assistance or guidance in the right direction will be most appreciated.

Was it helpful?

Solution

var numVal=Regex.Matches(input,@"\"([^\"]+)\"(\d+)")
                .Cast<Match>()
                .Select(x=>new
                 {
                     num=x.Groups[2].Value,
                     value=x.Groups[1].Value
                 });

Now you can iterate over numVal

foreach(var nv in numVal)
{
    nv.num;
    nv.value;
}

OTHER TIPS

If you're going straight to key value pairs, you might be able to use LINQ to make your life easier. You'll have to be aware of and handle cases where you don't match the key/value format. However, you might be able to achieve it using something like this.

var string_delimiter = new [] { ',' };
var kvp_delimiter = new[] { "\"" };

var dictionary = string_value.Split(string_delimiter)
                .Select(kvp_string => kvp_string.Split(kvp_delimiter, StringSplitOptions.RemoveEmptyEntries))
                .ToDictionary(kvp_vals => kvp_vals.First(), kvp_vals => kvp_vals.Last());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top