Page 1 of 1

Need a regex for this string

Posted: Tue Sep 25, 2007 8:08 pm
by legend986
I have this string:

"2 195.66.224.125 [AS 5459] 64 msec 60 msec 64 msec 3 209.85.252.42 [AS 15169] 60 msec 68 msec 209.85.252.40 [AS 15169] 60 msec (209.85.252.40) [AS 15169] 60 msec"

How can I actually extract just the ip address and the text in the brackets into an array?

I was thinking something like: [0-255].[0-255].[0-255].[0-255][ ][AS *] and [(][0-255].[0-255].[0-255].[0-255][)][ ][AS *] but I'm not sure this would work or how to put this to work. Can someone please tell me how to do it?

Posted: Wed Sep 26, 2007 1:03 am
by GeertDD

Code: Select all

#\d{1,3}(?:\.\d{1,3}){3}|(?<=\[)[^\]]*(?=\])#
Note that it also allows IPs like 999.999.999.999 currently. This can be solved, but I've not much time now and maybe this regex is sufficient already for your string.

Posted: Wed Sep 26, 2007 7:42 am
by superdezign

Code: Select all

((?:[0-2]?\d{1,2}\.){3}[0-2]?\d{1,2})\)?\s+(\[AS\s+\d*\])
That does a fairly good job of it. It doesn't ensure that there is a beginning parentheses when there is an ending parentheses, but it doesn't let the ending parentheses affect the retrieval. It allows 299.299.299.299 instead of up to 255, but it's good enough for me. :P

Posted: Wed Sep 26, 2007 8:34 am
by GeertDD
Just for the record, here's a regex that matches a number between 0 and 255:

Code: Select all

[01]?\d\d?|2(?:[0-4]\d|5[0-5])

Posted: Wed Sep 26, 2007 9:36 am
by feyd
GeertDD wrote:Just for the record, here's a regex that matches a number between 0 and 255:

Code: Select all

[01]?\d\d?|2(?:[0-4]\d|5[0-5])
It matches a few octal notations too. ;)

Posted: Wed Sep 26, 2007 1:13 pm
by GeertDD
feyd wrote:It matches a few octal notations too. ;)
Yeah, you could just drop the initial 0, however, I believe I once read IP addresses could have leading 0's in their segments. Whatever. :wink:

Code: Select all

1?\d\d?|2(?:[0-4]\d|5[0-5])

Posted: Wed Sep 26, 2007 2:27 pm
by feyd
GeertDD wrote:Yeah, you could just drop the initial 0, however, I believe I once read IP addresses could have leading 0's in their segments. Whatever. :wink:
IPv6 can indeed have leading zeros, but this regex isn't for IPv6. I don't recall IPv4 supporting leading zeros, but it's been a while since I read the spec.

Posted: Wed Sep 26, 2007 4:47 pm
by legend986
Thank you so much everyone... The one by superdezign most likely matches with the one i'm searching for...
superdezign wrote:

Code: Select all

((?:[0-2]?\d{1,2}\.){3}[0-2]?\d{1,2})\)?\s+(\[AS\s+\d*\])
............................................. enough for me. :P
I tried writing one myself after reading one of the guides present here and came up with this:

\(?[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\)?\s\[AS\s+\d*\]

Actually when I try this out at the following site:

Code: Select all

http://regexlib.com/RETester.aspx
I get something different. I've used these:

Source:

Code: Select all

1 217.150.52.174 0 msec 0 msec 0 msec
  2 195.66.224.125 [AS 5459] 176 msec 172 msec 176 msec
  3 209.85.252.42 [AS 15169] 184 msec 180 msec 180 msec
  4 216.239.43.123 [AS 15169] 188 msec 252 msec 176 msec
  5 64.233.175.246 [AS 15169] 184 msec 184 msec
    72.14.233.77 [AS 15169] 176 msec
  6 209.85.249.133 [AS 15169] 192 msec 196 msec 196 msec
  7 www.l.google.com (64.233.183.104) [AS 15169] 192 msec 184 msec
    209.85.249.129 [AS 15169] 192 msec
Pattern:

Code: Select all

\(?[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\)?\s\[AS\s+\d*\]
And the output I get is:

Code: Select all

Match
195.66.224.125 [AS 5459]
209.85.252.42 [AS 15169]
216.239.43.123 [AS 15169]
64.233.175.246 [AS 15169]
72.14.233.77 [AS 15169]
209.85.249.133 [AS 15169]
(64.233.183.104) [AS 15169]
209.85.249.129 [AS 15169]
When I use the same source with your pattern:

Code: Select all

((?:[0-2]?\d{1,2}\.){3}[0-2]?\d{1,2})\)?\s+(\[AS\s+\d*\])
I get this result:

Code: Select all

Match	$1	$2
195.66.224.125 [AS 5459]	195.66.224.125	[AS 5459]
209.85.252.42 [AS 15169]	209.85.252.42	[AS 15169]
216.239.43.123 [AS 15169]	216.239.43.123	[AS 15169]
64.233.175.246 [AS 15169]	64.233.175.246	[AS 15169]
72.14.233.77 [AS 15169]	72.14.233.77	[AS 15169]
209.85.249.133 [AS 15169]	209.85.249.133	[AS 15169]
64.233.183.104) [AS 15169]	64.233.183.104	[AS 15169]
209.85.249.129 [AS 15169]	209.85.249.129	[AS 15169]
See the columns, Match, $1 and $2? Well, I don't understand what they are. Could you please explain? Actually this is a much better way but I'm not understanding that. Infact, its very cool. Is there a way I could put $1 into $ip[0] and $2 into $ip[1] in php?

Posted: Mon Oct 08, 2007 7:52 am
by Schatten
You can also try this:

Code: Select all

$ip_chunk = '(?:[01]\d\d|2[0-4]\d|25[0-5]|\d{1,2})';
$ip = '\b(?:'.$ip_chunk.')(?:\.'.$ip_chunk.'){3}\b';
$text = '(?<=\[)[^]]+(?=\])';
preg_match_all('/'.$ip.'|'.$text.'/',$string,$matches);