Need a regex for this string

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
legend986
Forum Contributor
Posts: 258
Joined: Sun Jul 15, 2007 2:45 pm

Need a regex for this string

Post 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?
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post 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])
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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. ;)
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post 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])
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
legend986
Forum Contributor
Posts: 258
Joined: Sun Jul 15, 2007 2:45 pm

Post 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?
Schatten
Forum Newbie
Posts: 3
Joined: Mon Sep 24, 2007 10:23 am

Post 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);
Post Reply