Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Regexp to match IP address

by Anonymous Monk
on Oct 01, 2004 at 15:18 UTC ( [id://395635]=note: print w/replies, xml ) Need Help??


in reply to Re: Regexp to match IP address
in thread Regexp to match IP address

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-

Replies are listed 'Best First'.
Re^2: Regexp to match IP address
by petelock (Initiate) on Jun 02, 2013 at 09:18 UTC

    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 :)

Re^2: Regexp to match IP address
by Prior Nacre V (Hermit) on Oct 02, 2004 at 02:52 UTC

    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://395635]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2025-06-15 17:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.