验证IPv4 IP地址是否有效
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了验证IPv4 IP地址是否有效相关的知识,希望对你有一定的参考价值。
This code will validate not only the four octets contain between 1 and 3 numbers each, but also that the number they contain is between 0 and 255.In all, there are 3 different regex's that I tried and all seem to work fine. The three regex's are:
1. m/^(dd?d?).(dd?d?).(dd?d?).(dd?d?)/
2. m/dd?d?.dd?d?.dd?d?.dd?d?/
3. m/d{1,2}.d{1,3}.d{1,3}.d{1,3}/
The first one is used below. Enjoy!!!
#!/usr/bin/perl use strict; use warnings; my $ipaddr = <STDIN>; if( $ipaddr =~ m/^(dd?d?).(dd?d?).(dd?d?).(dd?d?)$/ ) { if($1 <= 255 && $2 <= 255 && $3 <= 255 && $4 <= 255) { } else { } } else { }
以上是关于验证IPv4 IP地址是否有效的主要内容,如果未能解决你的问题,请参考以下文章