Search code examples
c#.net-4.5fedex

Best Way to Parse A Fedex Transaction Reply String Using C#


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.


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