dqba94619 2013-01-29 03:00
浏览 130
已采纳

使用正则表达式PHP解析日志文件

I'm absolutely terrible at regex; can anyone help me solve the expression I need in order to separate two values I need from a log file?

Log file example.

1/28/2013 8:43:22 PM Removed        {178.76.234.41}
1/28/2013 8:43:22 PM Removed        {78.105.26.0}
1/28/2013 8:43:22 PM Removed        {24.165.198.12}
1/28/2013 8:43:23 PM Added          {178.76.234.41}
1/28/2013 8:43:23 PM Added          {69.246.227.43}

With my current code I am able to separate the IP address, however I now need both the state (added/removed) and the IP address. Here is my current code.

preg_match_all("/.*{(.*)}.*/", $a, $b);

What do I need to replace "/.{(.)}.*/" with in order to grab both the state and the IP address to store into the array?

  • 写回答

3条回答 默认 最新

  • dpm91915 2013-01-29 03:04
    关注

    If those are the only two words you need to include, have you tried something like this?

    preg_match_all("~(Removed|Added)\s+{(.*)}~i", $a, $b);
    

    So in total:

    $a = '1/28/2013 8:43:22 PM Removed        {178.76.234.41}
          1/28/2013 8:43:22 PM Removed        {78.105.26.0}
          1/28/2013 8:43:22 PM Removed        {24.165.198.12}
          1/28/2013 8:43:23 PM Added          {178.76.234.41}
          1/28/2013 8:43:23 PM Added          {69.246.227.43}';
    preg_match_all("~(Removed|Added)\s+{(.*)}~i", $a, $b);
    print_r($b);
    

    And resulting in this:

    Array ( [0] => Array ( [0] => Removed {178.76.234.41} [1] => Removed {78.105.26.0} [2] => Removed {24.165.198.12} [3] => Added {178.76.234.41} [4] => Added {69.246.227.43} ) [1] => Array ( [0] => Removed [1] => Removed [2] => Removed [3] => Added [4] => Added ) [2] => Array ( [0] => 178.76.234.41 [1] => 78.105.26.0 [2] => 24.165.198.12 [3] => 178.76.234.41 [4] => 69.246.227.43 ) )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里