You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

preg_match_all无法匹配游戏日志连续两行的问题排查与解决

从游戏日志中匹配带[HP: 0]的致命一击连续记录

需求

需要从游戏日志里提取特定的连续两行:

  • 第一行:带有[HP: 0]标记的玩家最后一击受击记录
  • 第二行:紧随其后的同玩家击杀记录
    (目的是确认该攻击为致命一击,因为玩家死亡后日志仍会记录后续攻击)

日志片段

21:12:44 | Player "MinecraftNerd8" (DEAD) (id=630DB6BBFCCC2E66508D086E78D202BFA4FB1FB5 pos=<5215.0, 13515.6, 204.5>)[HP: 0] hit by Player "sbog 9000" (id=EAD177E916EE7D01F6DC47C59EAF4802A6061420 pos=<5211.9, 13516.6, 203.8>) into Torso(11) for 35 damage (Bullet_12GaugePellets) with BK-43 from 3.32238 meters 
21:12:44 | Player "MinecraftNerd8" (DEAD) (id=630DB6BBFCCC2E66508D086E78D202BFA4FB1FB5 pos=<5215.0, 13515.6, 204.5>) killed by Player "sbog 9000" (id=EAD177E916EE7D01F6DC47C59EAF4802A6061420 pos=<5211.9, 13516.6, 203.8>) with BK-43 from 3.32238 meters 
21:12:44 | Player "RMuckey06" (DEAD) (id=1BE787EF788074C94B0FB22785EAD502E58240C1 pos=<5214.1, 13515.8, 203.8>)[HP: 0] hit by Player "sbog 9000" (id=EAD177E916EE7D01F6DC47C59EAF4802A6061420 pos=<5211.9, 13516.6, 203.8>) into Head(0) for 8.75 damage (Bullet_12GaugePellets) with BK-43 from 2.24871 meters 
21:12:44 | Player "RMuckey06" (DEAD) (id=1BE787EF788074C94B0FB22785EAD502E58240C1 pos=<5214.1, 13515.8, 203.8>) killed by Player "sbog 9000" (id=EAD177E916EE7D01F6DC47C59EAF4802A6061420 pos=<5211.9, 13516.6, 203.8>) with BK-43 from 2.24871 meters 
21:12:51 | Player "Cogito8434" (id=26DE70CAEF00AE579AF8CA21ED3F1648DB316F1E pos=<4629.4, 15078, 476.3>) regained consciousness

目标提取行

21:12:44 | Player "MinecraftNerd8" (DEAD) (id=630DB6BBFCCC2E66508D086E78D202BFA4FB1FB5 pos=<5215.0, 13515.6, 204.5>)[HP: 0] hit by Player "sbog 9000" (id=EAD177E916EE7D01F6DC47C59EAF4802A6061420 pos=<5211.9, 13516.6, 203.8>) into Torso(11) for 35 damage (Bullet_12GaugePellets) with BK-43 from 3.32238 meters 
21:12:44 | Player "MinecraftNerd8" (DEAD) (id=630DB6BBFCCC2E66508D086E78D202BFA4FB1FB5 pos=<5215.0, 13515.6, 204.5>) killed by Player "sbog 9000" (id=EAD177E916EE7D01F6DC47C59EAF4802A6061420 pos=<5211.9, 13516.6, 203.8>) with BK-43 from 3.32238 meters 

正则调试过程

  1. 最初尝试的正则(无匹配结果)
    问题:未对括号()、方括号[]等特殊字符转义,空格匹配逻辑存在问题,导致无法匹配日志内容。

    /(.*?)Player &quot;(.*)&quot; (DEAD) (id=(.*) pos=&lt;(.*)&gt;)[HP: 0] hit by Player &quot;(.*)&quot; (id=(.*) pos=&lt;(.*)&gt;) into (.*) for (.*) damage (.*) with (.*) from (.*) meters\s*
    (.*?)Player &quot;(.*)&quot; (DEAD) (id=(.*) pos=&lt;(.*)&gt;) killed by Player (.*)/
    
  2. 修改后的正则(无法过滤[HP:0])
    问题:虽然能匹配到数据,但没有精准锁定[HP: 0]的格式,无法过滤掉不符合条件的行。

    /(.*?) | Player &quot;(.*)&quot; (DEAD) (id=(.*) pos=&lt;(.*)&gt;) [HP:0] hit by Player &quot;(.*)&quot; (id=(.*) pos=&lt;(.*)&gt;) into (.*) for (.*) damage (.*) with (.*) from (.*) meters\s(.*?)Player &quot;(.*)&quot; (DEAD) (id=(.*) pos=&lt;(.*)&gt;) killed by Player (.*)/
    
  3. 最终正确正则
    调整点:对所有特殊字符(括号、方括号)进行转义,精准匹配[HP: 0]的格式,优化换行和空格的匹配逻辑,确保能准确捕获目标连续行。

    /(.*?)Player &quot;(.*)&quot; \(DEAD\)\ \(id=(.*) pos=&lt;(.*)&gt;\)\[HP: 0\] hit by Player &quot;(.*)&quot; \(id=(.*)\ pos=&lt;(.*)&gt;\) into (.*)\((.*)\) for(.*)damage (.*) with (.*) from (.*)\s*(.*) | Player &quot;(.*)&quot; \(DEAD\) (id=(.*)) (.*) killed by Player/
    

    该正则在大日志文件中测试通过,可成功匹配目标连续行并提取所需信息。

内容的提问来源于stack exchange,提问作者ChrisYates

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 19:24:52