dsoihsnz85757 2012-07-25 20:02
浏览 67
已采纳

正则表达式返回预期结果加上另一行的换行符

Before I start, I know this is CSV and I know there is a function that exist build-in PHP. I got the following pattern :

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

Who will parse something like that :

Country,Region/State,City,"Zip/Postal Code
 From","Zip/Postal Code To","Weight From","Weight To","Shipping Price","Delivery Type"

CAN,*,,,,0.0000,4999.0000,29.7500,Priority

CAN,*,,,,10000.0000,19999.0000,35.5000,Express

CAN,*,,,,0.0000,4999.0000,19.7500,Express

CAN,*,,,,20000.0000,99999999.9999,59.0000,Priority

CAN,*,,,,5000.0000,9999.0000,34.7500,Priority

CAN,*,,,,20000.0000,99999999.9999,41.5000,Express

CAN,*,,,,5000.0000,9999.0000,24.4500,Express

CAN,*,,,,10000.0000,19999.0000,48.0000,Priority

CAN,*,,,,10000.0000,19999.0000,29.7500,Standard

CAN,*,,,,20000.0000,99999999.9999,36.5000,Standard

CAN,*,,,,500.0000,9999.0000,20.3500,Standard

CAN,*,,,,90.0000,499.0000,9.7500,Standard

CAN,*,,,,50.0000,89.0000,1.8000,Standard

CAN,*,,,,30.0000,49.0000,1.5000,Standard

CAN,*,,,,0.0000,29.0000,1.0000,Standard

USA,*,,,,20000.0000,99999999.9999,160.0000,Express

USA,*,,,,10000.0000,14999.0000,76.0000,Express

USA,*,,,,1000.0000,4999.0000,42.0000,Express

USA,*,,,,15000.0000,19999.0000,155.0000,Priority

USA,*,,,,5000.0000,9999.0000,94.0000,Priority

USA,*,,,,0.0000,999.0000,75.5000,Priority

USA,*,,,,15000.0000,19999.0000,98.0000,Express

USA,*,,,,5000.0000,9999.0000,61.5000,Express

USA,*,,,,0.0000,999.0000,40.0000,Express

USA,*,,,,20000.0000,99999999.9999,230.0000,Priority

USA,*,,,,10000.0000,14999.0000,120.0000,Priority

USA,*,,,,1000.0000,4999.0000,61.5000,Priority

USA,*,,,,500.0000,999.0000,25.5000,Standard

USA,*,,,,90.0000,499.0000,13.3500,Standard

USA,*,,,,50.0000,89.0000,3.0000,Standard

USA,*,,,,30.0000,49.0000,1.8000,Standard

USA,*,,,,0.0000,29.0000,1.5000,Standard

The resulst I get is similar to :

[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
    (
    )

If I try to add ?: in the line break group it still do it. Can anyone help me, I am stuck there. Thanks.

  • 写回答

1条回答 默认 最新

  • dsrruefh12970 2012-07-25 22:09
    关注

    Not knowing any particulars of php matching, I'll take your word that the regex is working like you show it is (using my preferred regex I'm not capturing in the same way).

    I'll assume you are trying to remove those blank matches. I'll also believe that those "newlines" are actually encoded into the input, and not left as literal \'s and 's and 's.

    The problem seems to be the "newlines" are being matched twice? Like you match just the on one pass, and then the on the next pass?

    The simplest solution would be to restrict the newline to be the type you know the file has: /([^\"]|\"[^\"]*\")*?( )/ Does this help?

    Alternatively, I would just use a regex split (delimited by comma) on each line of input.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘