Need a regex for this string
Moderator: General Moderators
Need a regex for this string
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?
"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?
Code: Select all
#\d{1,3}(?:\.\d{1,3}){3}|(?<=\[)[^\]]*(?=\])#- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Code: Select all
((?:[0-2]?\d{1,2}\.){3}[0-2]?\d{1,2})\)?\s+(\[AS\s+\d*\])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])- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
It matches a few octal notations too.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])
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.feyd wrote:It matches a few octal notations too.
Code: Select all
1?\d\d?|2(?:[0-4]\d|5[0-5])- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.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.
Thank you so much everyone... The one by superdezign most likely matches with the one i'm searching for...
\(?[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:
I get something different. I've used these:
Source:
Pattern:
And the output I get is:
When I use the same source with your pattern:
I get this result:
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?
I tried writing one myself after reading one of the guides present here and came up with this:superdezign wrote:............................................. enough for me.Code: Select all
((?:[0-2]?\d{1,2}\.){3}[0-2]?\d{1,2})\)?\s+(\[AS\s+\d*\])
\(?[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.aspxSource:
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 msecCode: Select all
\(?[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\)?\s\[AS\s+\d*\]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]Code: Select all
((?:[0-2]?\d{1,2}\.){3}[0-2]?\d{1,2})\)?\s+(\[AS\s+\d*\])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]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);