【发布时间】:2012-02-08 07:16:17
【问题描述】:
这是一个非常复杂的正则表达式,它从专有的数据字符串中返回一组键/值对。这是数据的示例,以防express在.Net中无法使用而需要使用其他方法。
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
如果你仔细看,你会看到它的key,"value",key,"value" 格式化的唯一保证是每个键值对用逗号分隔,每个值将总是用双引号括起来。主要问题(你不能把它炸开的原因)是以前的编码器选择了错误的选择来分隔与条目具有相同字符的键和值。无论如何,不在我的手中。这是一个有效的 PHP 示例。
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
}
}
可以看到/(.*?),\"(.*?)\"/这个表达式
这是我在 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
【问题讨论】: