This is a pretty complex regular expression that returns an array of key/value pairs from a proprietary string of data. Here is sample of the data, in case the express can not be used in .Net and another method needs to be used.
0,"101"1,"12345"11,"ABC Company"12,"John Doe"13,"123 Main St"14,""15,"Malvern"16,"PA"17,"19355"19,"UPS"21,"10"22,"GND"23,""24,"082310"25,""26,"0.00"29,"1Z1235550300000645"30," PA 193 9-05"34,"6.55"37,"6.55"38,"8.05"65,"1Z1235550300000645"77,"10"96,""97,""98
If you look closely you see its key,”value“,key,”value” The only guarantee on formatting is that each key value pair is separated by a comma, and each value will always be encased in double quotes. The main problem (the reason you cant explode it) is the poor choice of the previous coder to separate keys and values with the same character as the entries. Anyways, out of my hands. Here is a working PHP example.
function parseResponse($response) {
// split response into $key, $value pieces
preg_match_all("/(.*?),\"(.*?)\"/", $response, $m);
// loop through pieces and format
foreach($m[1] as $index => $key) {
$value = $m[2][$index]
echo $key . ":" . $value;
// this will output KEY:VALUE for each entry in the string
}
}
You can see the expression /(.*?),\"(.*?)\"/
Here is what I have in VB .Net
Imports System.Text.RegularExpressions
Public Class Parser
Private Sub parseResponse(ByVal response As String)
Dim regExMatch As Match = Regex.Match(response, "/(.*?),\""(.*?)\""/")
End Sub
End Class
You need to remove the PHP delimiters:
Also, better be more specific about what can be matched (makes the regex much more efficient):
Now the first group only matches characters that aren’t commas, and the second one only matches characters that aren’t quotes. Both regexes would fail, by the way, if you were to have escaped quotes in your data.
To get all matches in a string, use