简体   繁体   English

正则表达式解析为特定模式

[英]Regex parsing for specific pattern

I have a huge logfile to parse through with PHP that looks for something like this (please note that variables that are variable will be labelled): 我有一个巨大的日志文件可以用PHP解析,看起来像这样(请注意,变量变量将被标记):

16:09:47.925 T#10648 PresenceManager: à¿šnoticing[specialchar]$name[specialchar]0x8fac711e4bf14e62-d-s111.221.74.46:40022-r[IP]:48914-l192.168.1.2:48914f2812a403bdc6ade

I want to be able to look for this line that contains $name, then parse out the [IP] part, which is an IP. 我希望能够查找包含$ name的行,然后解析出[IP]部分,这是一个IP。 Please note there's two special char places which I have marked that cannot be shown in the post. 请注意,我标记了两个特殊的字符位置,这些字符不能在帖子中显示。

This is what I have: 这就是我所拥有的:

if(preg_match('/' . $name . '*?-r(\d+\.\d+\.\d+\.\d+)/', $contents, $results))

However it doesn't seem to be finding the given ip like above D: 但是,似乎并没有像上面的D那样找到给定的ip:

Pastebinny:: http://pastebin.com/YHh4fndP Pastebinny :: http://pastebin.com/YHh4fndP

$log = https://mega.co.nz/#!Scc11A6K!RXziJU_Ii43o1gcQetEfS7Kfzt-bY7VTJXljpCS7Gfc (username is sliceteam) $ log = https://mega.co.nz/#!Scc11A6K!RXziJU_Ii43o1gcQetEfS7Kfzt-bY7VTJXljpCS7Gfc (用户名为sliceteam)

Thanks! 谢谢!

Now that I understand how the IP is found, try this regex : 现在,我了解了如何找到IP,请尝试以下正则表达式

/(?:variable.*?-r)((?:\d{1,3}\.){3}\d{1,3})/

variable is obviously where you would include the $name variable. variable显然是要包含$name变量的位置。 This uses a non capturing group to look for variable followed by any characters up to -r (the IP's preceder), and then capture an IP-like string. 它使用一个非捕获组来查找variable后跟直至-r (IP的优先级)的任何字符,然后捕获类似IP的字符串。 I defined an IP as 3 sets of 1-3 digits followed by a period followed by one final set of 1-3 digits. 我将IP定义为3组1-3位数字,后跟一个句点,再加上最后一组1-3位数字。

I hate to say it, but it seems like there is a different error in your script. 我讨厌这样说,但是您的脚本似乎有一个不同的错误。 I narrowed your code down to: 我将您的代码范围缩小到:

<?php
$name = 'sliceteam';
$log = (array_pop(glob('debug-20140405-1732.log')));
$contents = file_get_contents($log);
if (preg_match_all("/(?:$name.*?-r)((?:\d{1,3}\.){3}\d{1,3})/", $contents, $results))
{
    echo json_encode(array('status' => 'success', 'success' => $results[1]));
}
?>

And it returned {"status":"success","success":["168.62.23.92","213.146.168.254"]} (which seems pretty damn right to me ;)`). 它返回了{"status":"success","success":["168.62.23.92","213.146.168.254"]} (这对我来说似乎很糟糕;)`。 What do you receive when you run the entire script..and I can try to debug the problem with you. 当您运行整个脚本时,您会收到什么。我可以尝试与您一起调试问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM