Re: [Exim] Wish list: isIPaddress predicate

Góra strony
Delete this message
Reply to this message
Autor: Nick Cleaton
Data:  
Dla: Alun
CC: Exim users list
Temat: Re: [Exim] Wish list: isIPaddress predicate
On Fri, Sep 05, 2003 at 03:24:01PM +0100, Alun wrote:
>
> In /etc/exim/exim.pl:
>
> sub isipaddr
> {
>     return ((/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
>             && ($1 >= 0) && ($1 <= 255)
>             && ($2 >= 0) && ($2 <= 255)
>             && ($3 >= 0) && ($3 <= 255)
>             && ($4 >= 0) && ($4 <= 255)) ? "yes" : "no";


The >= 0 test is redundant since the regular expression doesn't allow
minus signs.

Also, '$' will match before a newline at the end of the string, so that
matches an IP address followed by a newline as well as matching an IP
address by itself.

How about:

sub isipaddr
{
    return ((/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\z/)
            && ($1 <= 255) && ($2 <= 255)
            && ($3 <= 255) && ($4 <= 255)) ? "yes" : "no";
}


--
Nick