<?php
/*
function:match string
author:
date:2003.7.15
parameter:
str=the string you want to match
return:
total=the string match the partern
*/
function matchstring($str) {
global $total;
preg_match_all("/,[^\-]\d*/",$str,$arr);
for($i=0; $i<count($arr[0]); $i++) {
$total[$i].= $arr[0][$i];
}
Return $total;
}
$str="1,-2,34,56,-78";
$str=",".$str;
$string=matchstring($str);
for($i=0; $i<count($string); $i++) {
$str_all.=$string[$i];
}
$array=explode(',',$str_all);
for($j=1; $j<count($array); $j++) {
$getstring.=$array[$j].',';

}
echo "$getstring";?>

解决方案 »

  1.   

    如果是比较数字那么不用正则也可以做到函数如下:
    <?
    /*  BEGIN function  功能:找出比0大的字符
    编写时间:2003.7.5
    编写人:偶然
    变量:
    string=要查找的字符串
    返回值:
    getstring=得到的字符串
    */
    function splitstr($string) {
    global $getstring;
    $array=explode(',',$string);
    for($i=0; $i<count($array); $i++) {
    if(((intval($array[$i])-intval(0))>=0)) {
    $getstring.=$array[$i].'|';
    }
    }

    Return $getstring;
    }
    /*  END function */$str="1,-2,34,56,-78";
    $string=splitstr($str);
    $array=explode('|',$string);
    for($i=0; $i<count($array); $i++) {
    $final.=$array[$i].' ';
    }
    echo $final;
    ?>
      

  2.   

    $str="1,-2,34,56,-78";
    // 把负数提取出来
    //preg_match_all("/(?=-)(-?\d+)/",$str,$arr); // goodname()(︿_︿)() 的原式
    preg_match_all("/(-\d+)/",$str,$arr); // 与之等价的式子
    print_r($arr);// 把正数提取出来
    //preg_match_all("/,(\d+)/",",$str",$arr); // 直观的解法,给$str前面补一个",",多个也无妨
    preg_match_all("/(?=^|,),?(\d+)/",$str,$arr); // 不容易理解的写法
    print_r($arr);
      

  3.   

    抠一下,我的正则不好:{
    数学上允许的--78
    $str="1,-2,34,56,--78";
      

  4.   

    抠一下,我的正则不好:{
    数学上允许的--78
    $str="1,-2,34,56,--78";
      

  5.   

    --78是否应该被认为是一个正数,如果是那么用我上面给出的splitstr函数可以得到;
    如果不是那就用这个吧,根据唠叨兄的正则写的。
    <?php
    /*
    function:match string
    author:
    date:2003.7.15
    parameter:
    str=the string you want to match
    return:
    total=the string match the partern
    */
    function matchstring($str) {
    global $total;
    preg_match_all("/(?=^|,),?(\d+)/",$str,$arr);
    for($i=0; $i<count($arr[0]); $i++) {
    $total[$i].= $arr[0][$i];
    }
    Return $total;
    }
    $str="1,-2,34,56,--78";
    $str=",".$str;
    $string=matchstring($str);
    for($i=0; $i<count($string); $i++) {
    $str_all.=$string[$i];
    }
    $array=explode(',',$str_all);
    for($j=1; $j<count($array); $j++) {
    $getstring.=$array[$j].',';

    }
    echo "$getstring";?>
      

  6.   

    -78 是负数
    --78 若视为正数
    那么
    ---78 是负数了?
    ----78 又是正数了吧?正则表达式不能计数。分割成串的话也需要用substr_count($s,"-")来统计“-”出现的次数来决定是正还是负吧
    不过这在php中是不成立的,如$a=--78;立即就会报错
      

  7.   

    楼主的问题相信已经解决了.
    xuzuning(唠叨):说得好,--78是我抠出来的,没什么意义.
    $a=--78;这到没有想过,
    $a = -(-78);可以
    这里关键是从字符串中如何取出-2,不取出--78的问题
    $str="1,-2,34,56,--78";
    也就是说:负号为奇数个的问题.我的正则不好,相信正则能办的到.
      

  8.   

    preg_match_all("/(?=^|,),?(\d+)/",$str,$arr); 
    呵呵,这一个好,是我想要得那种!
    我想到(?=,)这样了,但是不知道为什么(?=^|,)这个就可以解决,
      

  9.   

    其实都理解了就是说不好。
    ^是开头,在[]中是否定
    $str="1,-2,34,56,-78";
    主要的问题是如何匹配开始的那个1
    我通常写作
    preg_match_all("/(^|,)(\d+)/",$str,$arr);
    这样需用$arr[2]取得结果,而
    preg_match_all("/(?=^|,),?(\d+)/",$str,$arr); 
    更符合楼主的意思
    (?=^|,) 是声明 "^"或"," 有点类似[ab] 或 a|b
    不大容易说明白
      

  10.   

    关于多个-的问题,我是这个解决的,比较苯,不知道能否简化一下:preg_match_all("/(?=^|,),?((?:--)*\d+)/",$str,$arr);
      

  11.   

    应该这样吧?
    preg_match_all("/(?=^|,),?(?:--)*(\d+)/",$str,$arr);这都是从哪里看来的?
      

  12.   

    呵呵,自己想的。
    如果总是重复两个--,自然就抵消了。
    这几天一直想这个式子,给难住了。我的那个式子是按照,分隔的数组原样取得的
    而你给出的是化简抵消之后的。就是不知道如何匹配比如偶数个--只能这么写么
    (--)*匹配abcabcabc的,只能(abc){3}么,只能用括号来匹配重复的字符串?
    ???
      

  13.   

    是的。如果你不想让()中的内容出现在结果集中可使用?:
    (?:abc){3}在看这个,给日期中不足两位的前面补0
    function a($s){
    //  return preg_replace("/-(\d)(?=\D|$)/","-0\\1",$s) . "<br>";
      return str_replace("_0","0",preg_replace("/([-: ])(\d)(?=\D|$)/","\\1_0\\2",$s)) . "<br>";
    }
    echo a("2003-1-6 12:1:1");
    echo a("2003-10-6 2:1:5");
    echo a("2003-1-16 22:7:5");
      

  14.   

    呵呵,解决了,原来这个问题是如此的简单,:(,看看这个是不是你想要得:preg_replace("/([-: ])(\d)(?=\D|$)/e","'\\1'.'0'.'\\2'",$s)