dongyang7152 2010-05-01 00:32
浏览 40
已采纳

正则表达大师! 这是一个预告片:混合千位分隔符和csv

I've got a string like...

"labour 18909, liberals 12,365,conservatives 14,720"

...and i'd like a regex which can get rid of any thousands separators so i can pull out the numbers easily. Or even a regex which could give me a tidy array like:

(labour => 18909, liberals => 12365, conservatives => 14720)

Oh i wish i had the time to figure out regexes! Maybe i'll buy one as a toilet book, mmm.

  • 写回答

5条回答 默认 最新

  • duanmi4379 2010-05-01 01:02
    关注

    Two-liner. Will also get Independents:

    preg_match_all('/([a-zA-Z]+)\s*([\d,]+)(?:,|$)/', $str, $matches);
    $totals = array_combine($matches[1], $matches[2]);
    
    /* total:
    Array
    (
        [labour] => 18909
        [liberals] => 12,365
        [conservatives] => 14,720
    )
    */
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大