Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将此PHP正则表达式匹配模式转换为.Net_Php_.net_Regex_Vb.net - Fatal编程技术网

如何将此PHP正则表达式匹配模式转换为.Net

如何将此PHP正则表达式匹配模式转换为.Net,php,.net,regex,vb.net,Php,.net,Regex,Vb.net,这是一个非常复杂的正则表达式,它从专有数据字符串返回一个键/值对数组。这是数据示例,以防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

这是一个非常复杂的正则表达式,它从专有数据字符串返回一个键/值对数组。这是数据示例,以防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
如果仔细观察,您会看到它的
,“
”,
,“
”,格式化的唯一保证是每个键值对都用逗号分隔,并且每个值总是用双引号括起来。主要问题(您无法分解它的原因)是以前的编码器选择不当,无法将与条目具有相同字符的键和值分开。不管怎样,都是我的事。下面是一个正常工作的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

您需要删除PHP分隔符:

Dim RegexObj As New Regex("(.*?),""(.*?)""")
此外,最好更具体地说明可以匹配什么(使正则表达式更有效):

现在,第一组只匹配非逗号的字符,第二组只匹配非引号的字符。顺便说一下,如果数据中有转义引号,两个正则表达式都会失败

要获取字符串中的所有匹配项,请使用

AllMatchResults = RegexObj.Matches(response)

这不是正常工作,它似乎只返回第一个项目,我尝试了一个for/each没有运气。我需要一次一个地处理这些键/值对,就像在php示例中一样。@MikeL:您需要的是
.Matches()
方法,而不是
.Match()
。谢谢!我们在同一时间计算出了…对于每一个m作为匹配中的匹配
AllMatchResults = RegexObj.Matches(response)