Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Regexp to match IP address

by Prior Nacre V (Hermit)
on Aug 06, 2004 at 19:13 UTC ( [id://380673]=note: print w/replies, xml ) Need Help??


in reply to Regexp to match IP address

Or maybe:

defined((grep { $_ =~ /^\d{1,3}$/ && $_ <= 255 } split /\./, $ip_addr, + 4)[3])
Update
Had 127 instead of 225 above

Regards,

PN5

Replies are listed 'Best First'.
Re: Regexp to match IP address
by Anonymous Monk on Oct 01, 2004 at 15:18 UTC
    In javascript i did that:
    re=/^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([1-9]|[1-9] +[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}\$/; if(re.test(document.forms[0].ipv4.value)==false){ valid=false; message+="Wrong IP address !\n"; }
    I think it's possible to match the same regexp in perl.

    it seams to work...

    Regards,
    Orion-

      I like this one using quoted reg ex

      my $ip = '192.168.255.254'; # for example # Set up the reg ex my $ipno = qr/ 2(?:5[0-5] | [0-4]\d) | 1\d\d | [1-9]?\d /x; # To test if ( $ip =~ /^($ipno(?:\.|$)){4}/ ){ print "IP OK\n"; };

      I went for an interview at Arm in Cambridge, UK. They asked me to write one on the board and I wrote some lame one .. and later ... brooding on my poor attempt set out to make a better one. Driven by failure? Or maybe just really irritated by it. I still got the job :)

      I haven't used Javascript for 4 or 5 years. Check up-to-date documentation where appropriate.

      I believe Javascript1.3 REs were the same as Perl4's. More recent JS versions may map to more recent Perl versions. However, while the latest version may provide the best solution, consider whether your intended audience will have browsers incorporating the latest JS version.

      Any testing you do on the client side (e.g. Javascript in HTML) must be repeated on the server side (e.g. Perl CGI script). If you can use the same RE in both, this is possibly a good idea for consistency purposes; other considerations may override this.

      I don't think your RE matches 0.n.n.n (from visual inspection). Was this intended? If not, I think you'll need: re=/^([0-9]|...

      Finally, I recall running into difficulties due to differences between JavaScript, JScript and ECMAscript. Something to bear in mind if you're using advanced features.

      Regards,

      PN5

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-26 09:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found