下面是待处理的字符串
 "ccc   ddd  eee Node.sum=A1:cpu=16+A2:cpu=18+A3:cpu=22+A4:cpu=15 other..."
其中我有兴趣的是字符串
Node.sum=A1:cpu=16+A2:cpu=18+A3:cpu=22+A4:cpu=15 (后面可能还有+的样子其中"Node.sum"是一个特定标记
如果找到这个标记后 后面可以按以下方式分隔
Node.sum=“A1:cpu=16”+“A2:cpu=18”+“A3:cpu=22”+“A4:cpu=15”可后面还以再加上其他的值
我现在对于找到这个标记的语句  需要计算其中的16+18+22+15我想要一个完整的处理方法 包括从原来的“ccc   ddd  eee Node.sum=A1:cpu=16+A2:cpu=18+A3:cpu=22+A4:cpu=15 other..."
”提取出Node.sum=A1:cpu=16+A2:cpu=18+A3:cpu=22+A4:cpu=15再处理的方法我想用正则的 但写了半天没搞定   又是初学PHP 项目逼的很近 所以来这里求助大家

解决方案 »

  1.   

    other 是什么 意思是还有更多 还是本来字符串就是有other? 
    "是自己加上去的还是字符串结尾?
      

  2.   


    <?php
    $str = "ccc ddd eee Node.sum=A1:cpu=16+A2:cpu=18+A3:cpu=22+A4:cpu=15 other...";
    preg_match('/Node.sum=[\w:=+]*/',$str,$arr);
    $new = $arr[0];
    preg_match_all('/\d{1,}(?=\+)/',$new,$arr2);
    print_r($arr2);
    ?>
      

  3.   

    我运行了下 
    最后一个元素 cpu=15不在数组中?
    还不能结贴啊
      

  4.   

    other就是后面是其他的字符串Node.sum=A1:cpu=16+A2:cpu=18+A3:cpu=22+A4:cpu=15 (后面可能还有+的样子
    就是说也可能是
    Node.sum=A1:cpu=16+A2:cpu=18+A3:cpu=22+A4:cpu=15+A5:cpu=33
    对于上面的这个
    我就要计算16+18+22+15+33
      

  5.   

    顶 
    heyli再帮忙看下  最后一个结果没加进去  
      

  6.   

    楼上的有思路没?
    我这也被项目赶的没辙了
    以前都是搞C的
    这个地方想 用字符串查找 但处理的文本太多 又担心效率  不会perl  急忙看了几天书 就着急上手了
      

  7.   

    随便给你个交作业吧....正则我不是很熟悉$str = "ccc ddd eee Node.sum=A1:cpu=16+A2:cpu=18+A3:cpu=22+A4:cpu=15+A5:cpu=33";
    preg_match('/Node.sum=[\w:=+]+/',$str,$arr);
    $new = $arr[0];
    preg_match_all('/=([0-9]+)/',$new,$arr2);
    print_r($arr2[1]);
      

  8.   

    PhpNewnew
    你这个模式不行
    Node.sum=16 这种情况就不是我想要的规则
    不管怎么样 我先结贴了 晚上一会看下正则 看能不能写出来
      

  9.   


    不知道你有这个情况那么
    $str = "ccc ddd eee Node.sum=16+A1:cpu=16+A2:cpu=18+A3:cpu=22+A4:cpu=15+A5:cpu=33";
    preg_match('/Node.sum=[\w:=+]+/',$str,$arr);
    $new = $arr[0];
    //Node.sum=16+A1:cpu=16+A2:cpu=18+A3:cpu=22+A4:cpu=15+A5:cpu=33
    preg_match_all('/A[0-9]?\:cpu=([0-9]+)/',$new,$arr2);
    print_r($arr2[1]);可以吗?如果cpu不是固定的你可以换成[a-z]+