问个问题,我想分别提取Jan 23 12:57:05 siweiredhat useradd[1980]: new user: name=t1, uid=505, gid=505, home=/home/t1, shell=/bin/bash"
这段内容里的时间(Jan 23 12:57:05),主机名(siweiredhat)
操作(useradd[1980]:)和后面描述中的new user: name=t1这部分
  我用的方法是:
        <?php
$notes = "
Jan 23 12:57:05 siweiredhat useradd[1980]: new user: name=t1, uid=505, gid=505, home=/home/t1, shell=/bin/bash";
 
preg_match_all('/([\w]{3}[\s]+[\d]{2}[\s]+[\d:]{8})[\s]+(siweiredhat)[\s]+(useradd\[[\d]+\]?:)[\s]+(.+name=.+)/',$notes,$a);
 ?>
 显示结果为:Array ( [0] => Array ( [0] => Jan 23 12:57:05 siweiredhat useradd[1980]: new user: name=t1, uid=505, gid=505, home=/home/t1, shell=/bin/bash ) [1] => Array ( [0] => Jan 23 12:57:05 ) [2] => Array ( [0] => siweiredhat ) [3] => Array ( [0] => useradd[1980]: ) [4] => Array ( [0] => new user: name=t1, uid=505, gid=505, home=/home/t1, shell=/bin/bash ) )    uid=505, gid=505, home=/home/t1, shell=/bin/bash这部分还是显示出来了,请问怎样写,可以屏蔽掉(uid=505, gid=505, home=/home/t1, shell=/bin/bash)这部分。

解决方案 »

  1.   

    preg_match_all('/([\w]{3}[\s]+[\d]{2}[\s]+[\d:]{8})[\s]+(siweiredhat)[\s]+(useradd\[[\d]+\]?:)[\s]+(.+name=.{2})/',$notes,$a); 
      

  2.   

    Jan   23   12:57:05   siweiredhat   useradd[1980]:   new   user:   name=t1,   uid=505,   gid=505,   home=/home/t1,   shell=/bin/bash"是单独一行不?是的话
    /^[\s\S]+(?=,\s*uid)/
      

  3.   

    split 之后join起来就可以了。比一定非要用正则
      

  4.   


    echo "hello php";