1

在开始之前,我知道这是 CSV,并且我知道有一个内置 PHP 的函数。我得到了以下模式:

preg_match_all("/([^\"]|\"[^\"]*\")*?(r\n|\n\r|\r|\n)/i", $CSV, $Matches);

谁会解析这样的东西:

Country,Region/State,City,"Zip/Postal Code\n From","Zip/Postal Code To","Weight From","Weight To","Shipping Price","Delivery Type"\n\r
CAN,*,,,,0.0000,4999.0000,29.7500,Priority\n\r
CAN,*,,,,10000.0000,19999.0000,35.5000,Express\n\r
CAN,*,,,,0.0000,4999.0000,19.7500,Express\n\r
CAN,*,,,,20000.0000,99999999.9999,59.0000,Priority\n\r
CAN,*,,,,5000.0000,9999.0000,34.7500,Priority\n\r
CAN,*,,,,20000.0000,99999999.9999,41.5000,Express\n\r
CAN,*,,,,5000.0000,9999.0000,24.4500,Express\n\r
CAN,*,,,,10000.0000,19999.0000,48.0000,Priority\n\r
CAN,*,,,,10000.0000,19999.0000,29.7500,Standard\n\r
CAN,*,,,,20000.0000,99999999.9999,36.5000,Standard\n\r
CAN,*,,,,500.0000,9999.0000,20.3500,Standard\n\r
CAN,*,,,,90.0000,499.0000,9.7500,Standard\n\r
CAN,*,,,,50.0000,89.0000,1.8000,Standard\n\r
CAN,*,,,,30.0000,49.0000,1.5000,Standard\n\r
CAN,*,,,,0.0000,29.0000,1.0000,Standard\n\r
USA,*,,,,20000.0000,99999999.9999,160.0000,Express\n\r
USA,*,,,,10000.0000,14999.0000,76.0000,Express\n\r
USA,*,,,,1000.0000,4999.0000,42.0000,Express\n\r
USA,*,,,,15000.0000,19999.0000,155.0000,Priority\n\r
USA,*,,,,5000.0000,9999.0000,94.0000,Priority\n\r
USA,*,,,,0.0000,999.0000,75.5000,Priority\n\r
USA,*,,,,15000.0000,19999.0000,98.0000,Express\n\r
USA,*,,,,5000.0000,9999.0000,61.5000,Express\n\r
USA,*,,,,0.0000,999.0000,40.0000,Express\n\r
USA,*,,,,20000.0000,99999999.9999,230.0000,Priority\n\r
USA,*,,,,10000.0000,14999.0000,120.0000,Priority\n\r
USA,*,,,,1000.0000,4999.0000,61.5000,Priority\n\r
USA,*,,,,500.0000,999.0000,25.5000,Standard\n\r
USA,*,,,,90.0000,499.0000,13.3500,Standard\n\r
USA,*,,,,50.0000,89.0000,3.0000,Standard\n\r
USA,*,,,,30.0000,49.0000,1.8000,Standard\n\r
USA,*,,,,0.0000,29.0000,1.5000,Standard\n\r

我得到的结果类似于:

[2] => Array
    (
    )

[3] => Array
    (
        [0] => CAN
        [1] => *
        [2] => 
        [3] => 
        [4] => 
        [5] => 10000.0000
        [6] => 19999.0000
        [7] => 35.5000
    )

[4] => Array
    (
    )

[5] => Array
    (
        [0] => CAN
        [1] => *
        [2] => 
        [3] => 
        [4] => 
        [5] => 0.0000
        [6] => 4999.0000
        [7] => 19.7500
    )

[6] => Array
    (
    )

如果我尝试?:在换行组中添加它仍然会这样做。任何人都可以帮助我,我被困在那里。谢谢。

4

1 回答 1

1

不知道 php 匹配的任何细节,我会相信正则表达式的工作方式就像你展示的那样(使用我喜欢的正则表达式,我不会以同样的方式捕获)。

我假设您正在尝试删除那些空白匹配项。我也相信那些“换行符”实际上被编码到输入中,而不是作为文字\'s 和\r's 和\n's。

问题似乎是“换行符”被匹配了两次?就像您只匹配\n一次传球,然后匹配\r下一次传球?

最简单的解决方案是将换行符限制为您知道文件具有的类型:/([^\"]|\"[^\"]*\")*?(\n\r)/ 这有帮助吗?

或者,我会在每行输入上使用正则表达式拆分(用逗号分隔) 。

于 2012-07-25T22:09:31.200 回答