Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Regular Expression to Match IP Address

by Dietz (Curate)
on Oct 03, 2004 at 13:06 UTC ( [id://396001]=note: print w/replies, xml ) Need Help??


in reply to Regular Expression to Match IP Address

This should work for your needs:
my $range = qr/^ ( (?: # first 3 octets: (?: 2(?:5[0-5]|[0-4][0-9])\. ) # 200 - 255 | # or (?: 1[0-9][0-9]\. ) # 100 - 199 | # or (?: (?:[1-9][0-9]?|[0-9])\. ) # 0 - 99 ) {3} # above: three times (?: # 4th octet: (?: 2(?:5[0-5]|[0-4][0-9]) ) # 200 - 255 | # or (?: 1[0-9][0-9] ) # 100 - 199 | # or (?: [1-9][0-9]?|[0-9] ) # 0 - 99 ) $) /x; print "OK\n" if ($ip =~ /$range/);

Update:
Changed (?: [0-9][0-9]? ) to (?: [1-9][0-9]?|[0-9] ) so that it won't accidently match an octet with only 2 or more zeros or an octet with leading zero (i.e. 00.0.0.0 or 192.186.0.01)
Now this will only match addresses from 0.0.0.0 to 255.255.255.255 (assuming the address in $ip is already chomped)

Replies are listed 'Best First'.
Re^2: Regular Expression to Match IP Address
by codecookers (Initiate) on Mar 16, 2010 at 12:26 UTC
    if we want a simple version print"\n ip address " if ($s=~/((\d){1,3}\.){3}(\d){1,3}/) ;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://396001]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-19 21:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found