user3500524
user3500524

Reputation: 3

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):

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. 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:

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

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

Thanks!

Upvotes: 0

Views: 68

Answers (1)

Sam
Sam

Reputation: 20486

Now that I understand how the IP is found, try this regex:

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

variable is obviously where you would include the $name variable. 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. I defined an IP as 3 sets of 1-3 digits followed by a period followed by one final set of 1-3 digits.

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 ;)`). What do you receive when you run the entire script..and I can try to debug the problem with you.

Upvotes: 1

Related Questions