one more IP addr regexp

V

Vasily Pomuran

Hi everybody.
It seems that this regexp works right.

/\b((\d|1{0,1}\d{2}|2[01234]\d|25[012345])\.){3}(\d|1{0,1}\d{2}|2[01234]\d|25[012345])\b/

Maybe you'll find it useful.

Best Regards

V.Pomuran
 
T

Tintin

Vasily Pomuran said:
Hi everybody.
It seems that this regexp works right.

/\b((\d|1{0,1}\d{2}|2[01234]\d|25[012345])\.){3}(\d|1{0,1}\d{2}|2[01234]\d|25[012345])\b/

Maybe you'll find it useful.

Would be useful if it matched valid IP addresses.

1.1.1.1.1 is not a valid IP address.
 
C

Chris Mattern

Tintin said:
Vasily Pomuran said:
Hi everybody.
It seems that this regexp works right.

/\b((\d|1{0,1}\d{2}|2[01234]\d|25[012345])\.){3}(\d|1{0,1}\d{2}| [01234]\d|25[012345])\b/

Maybe you'll find it useful.

Would be useful if it matched valid IP addresses.

1.1.1.1.1 is not a valid IP address.

Personally, I just use inet_aton(). Why reinvent the wheel?

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
G

Gunnar Hjalmarsson

Tintin said:
Vasily said:
/\b((\d|1{0,1}\d{2}|2[01234]\d|25[012345])\.){3}(\d|1{0,1}\d{2}|2[01234]\d|25[012345])\b/

Would be useful if it matched valid IP addresses.

1.1.1.1.1 is not a valid IP address.

It doesn't match that either.

my $re =
qr/\b((\d|1{0,1}\d{2}|2[01234]\d|25[012345])\.){3}(\d|1{0,1}\d{2}|2[01234]\d|25[012345])\b/;
my $ip = '1.1.1.1.1';
print "$1\n" if $ip =~ /($re)/;

Outputs:
1.1.1.1
 
C

Chris Mattern

Gunnar said:
Tintin said:
Vasily said:
/\b((\d|1{0,1}\d{2}|2[01234]\d|25[012345])\.){3}(\d|1{0,1}\d{2}|
[01234]\d|25[012345])\b/

Would be useful if it matched valid IP addresses.

1.1.1.1.1 is not a valid IP address.

It doesn't match that either.

my $re =
qr/\b((\d|1{0,1}\d{2}|2[01234]\d|25[012345])\.){3}(\d|1{0,1}\d{2}| [01234]\d|25[012345])\b/;
my $ip = '1.1.1.1.1';
print "$1\n" if $ip =~ /($re)/;

Outputs:
1.1.1.1
You want to try to edit more or less randomly things that aren't valid
IPs so that they'll be valid IPs? NOT a good idea, IMHO.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
G

Gunnar Hjalmarsson

Chris said:
Gunnar said:
Tintin said:
Would be useful if it matched valid IP addresses.

1.1.1.1.1 is not a valid IP address.

It doesn't match that either.

my $re =
qr/\b((\d|1{0,1}\d{2}|2[01234]\d|25[012345])\.){3}(\d|1{0,1}\d{2}|[01234]\d|25[012345])\b/;
my $ip = '1.1.1.1.1';
print "$1\n" if $ip =~ /($re)/;

Outputs:
1.1.1.1

You want to try to edit more or less randomly things that aren't valid
IPs so that they'll be valid IPs?

Didn't say that. All I said was that it doesn't match '1.1.1.1.1'. :)
 
T

Tintin

Gunnar Hjalmarsson said:
Tintin said:
Vasily said:
/\b((\d|1{0,1}\d{2}|2[01234]\d|25[012345])\.){3}(\d|1{0,1}\d{2}|2[01234]\d|25[012345])\b/

Would be useful if it matched valid IP addresses.

1.1.1.1.1 is not a valid IP address.

It doesn't match that either.

my $re =
qr/\b((\d|1{0,1}\d{2}|2[01234]\d|25[012345])\.){3}(\d|1{0,1}\d{2}|2[01234]\d|25[012345])\b/;
my $ip = '1.1.1.1.1';
print "$1\n" if $ip =~ /($re)/;

Outputs:
1.1.1.1

Depends on how you use the regex.

If you say match, you are generally doing:

if (/some regex/) {
print "matched\n";
}
else {
print "not matched\n";
}

Anyway, I like using
http://search.cpan.org/~adamk/Validate-Net-0.5/lib/Validate/Net.pm
for validating IP addresses.
 
B

Big and Blue

Would be useful if it matched valid IP addresses.

572662306 is a valid IP address.

As is 04210421042.

And 0x22222222.

And 34.34.8738 (I could go on...)

Try telnet'ing to them. You'll see they all (try to) connect to
34.34.34.34.

If you are trying to match dotted-quad addresses fine, but don't think
that corresponds to all "IP addresses".
 
M

Martin Kissner

Big and Blue wrote :
572662306 is a valid IP address.

As is 04210421042.

And 0x22222222.

And 34.34.8738 (I could go on...)

Try telnet'ing to them. You'll see they all (try to) connect to
34.34.34.34.

Hm. Are you sure?

% telnet 572662306
572662306: No address associated with nodename
% telnet 04210421042.
04210421042.: No address associated with nodename
% telnet 0x22222222.
0x22222222.: No address associated with nodename
% telnet 0x22222222
0x22222222: No address associated with nodename
% telnet 34.34.8738
34.34.8738: No address associated with nodename

Doesn't work on my host.

Regards
Martin
 
C

Chris Mattern

Martin said:
Big and Blue wrote :

Hm. Are you sure?

% telnet 572662306
572662306: No address associated with nodename
% telnet 04210421042.
04210421042.: No address associated with nodename
% telnet 0x22222222.
0x22222222.: No address associated with nodename
% telnet 0x22222222
0x22222222: No address associated with nodename
% telnet 34.34.8738
34.34.8738: No address associated with nodename
Does depend on your telnet client. My Linux system gave me this:

syscjm@ayato:~$ telnet 572662306
telnet: could not resolve 572662306/telnet: Name or service not known
syscjm@ayato:~$ telnet 04210421042
telnet: could not resolve 04210421042/telnet: Name or service not known
syscjm@ayato:~$ telnet 0x22222222
telnet: could not resolve 0x22222222/telnet: Name or service not known
syscjm@ayato:~$ telnet 34.34.8738
telnet: could not resolve 34.34.8738/telnet: Name or service not known

But my Solaris system gave me this:

$ telnet 572662306
Trying 34.34.34.34...
^C$ telnet 04210421042
Trying 34.34.34.34...
^C$ telnet 0x22222222
Trying 34.34.34.34...
^C$ telnet 34.34.8738
Trying 34.34.34.34...

And an AIX system gave me this:

$ telnet 572662306
Trying...
$ telnet 04210421042
Trying...
$ telnet 0x22222222
Trying...
$ telnet 34.34.8738
Trying...

(not as informative, but it pretty much has to be trying 34.34.34.34)

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
C

Chris Mattern

Abigail said:
Big and Blue ([email protected]) wrote on MMMMCLXXX September MCMXCIII
in <URL:''
'' Try telnet'ing to them. You'll see they all (try to) connect to
'' 34.34.34.34.


So, "localhost" is a valid IP address as well?
No, because it doesn't work if the process can't do name resolution.
The numbers B&B gave will (in most implementation of telnet, at least).

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
A

Anno Siegel

Gunnar Hjalmarsson said:
Chris said:
Gunnar said:
Tintin wrote:
Would be useful if it matched valid IP addresses.

1.1.1.1.1 is not a valid IP address.

It doesn't match that either.

my $re =
qr/\b((\d|1{0,1}\d{2}|2[01234]\d|25[012345])\.){3}(\d|1{0,1}\d{2}|[01234]\d|25[012345])\b/;
my $ip = '1.1.1.1.1';
print "$1\n" if $ip =~ /($re)/;

Outputs:
1.1.1.1

You want to try to edit more or less randomly things that aren't valid
IPs so that they'll be valid IPs?

Didn't say that. All I said was that it doesn't match '1.1.1.1.1'. :)

It should match the first valid IP address it meets. So far it's up
to specification as a regex. I haven't looked closely at it, it's much
too uninviting. A regex isn't a good tool to check numeric ranges,
and it shows. You don't see what checks what, so it's hard to tell
if it's correct.

If this must be done, write a simple regex that matches anything that
vaguely looks like an IP addr. /[.\d]+/ might do, but you could get more
sophisticated, still keeping the regex simple. Then have a water-proof
checker that decides if a string is really valid. Write that based on
split and numeric comparisons, so it's easy to see what checks are done
where. Combine the two as needed to efficiently extract any number of IP
addresses from any amount of text.

Or use an industrial strength regex from Regex::Common.

Anno
 
B

Big and Blue

Abigail said:
So, "localhost" is a valid IP address as well?

No - it's a(n interface) name.

That's the difference between the "a" and the "n" side in inet_aton()
and inet_ntoa().
 
B

Big and Blue

Chris said:
No, because it doesn't work if the process can't do name resolution.
The numbers B&B gave will (in most implementation of telnet, at least).

Indeed, IIRC the RFC says they should. (Well, it might actually say
MAY, SHOULD or MUST, but I haven't read it recently :)).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,167
Messages
2,570,913
Members
47,455
Latest member
Delilah Code

Latest Threads

Top