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 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教