Perl : Numeric Range Pattern Matching

hi Experts

just wondering if you can help me check a number between a specific range

if i have an ip address , how can i say the valid number for ip between 1 to 254

something like this

if ($ip ) =~ /[1-254].[1-254].[1-254].[1-254]/
{

}

what the pattern i need to type

thanks

You can try this:

$ip=~/(\d+)\.(\d+)\.(\d+)\.(\d+)/;
if ($1>=0 && $1<=255 && $2>=0 && $2<=255 && $3>=0 && $3<=255 && $4>=0 && $4<=255)
{
}
1 Like

thanks ... it works

just wondering if i can do it in the patten matching

It can be done, but it is not pretty :slight_smile:

$ip=~/\b((2[0-5][0-5]\.)|(1?[0-9]?[0-9]\.)){3}((2[0-5][0-5])|(1?[0-9]?[0-9]))\b/
2 Likes