Jump to content
Sign in to follow this  
Moze

Question about StringRegExp

Recommended Posts

Moze

Hello i'm kinda noob at programing and trying to make function that will check if string is valid IP address. i use StringRegExp. At moment pattern that i wrote is:

StringRegExp("192.168.1.1", '\A[1-255]\.[0-255]\.[0-255]\.[0-255]\z', 0)
But it does not work. Please help me to make a good pattern that will match a valid ip address.

P.S. English isn't my native language...

Edited by Moze

Share this post


Link to post
Share on other sites
weaponx

There is an example of this on the regular-expressions.info example page:

http://www.regular-expressions.info/examples.html

Share this post


Link to post
Share on other sites
weaponx

You could also say "Screw regular expressions!":

$testip = "300.50.50.50"

MsgBox(0,"",validIP($testIP))

;Return 0 if valid
;Return 1 if ip has too many or too few bytes
;Return 2 if any byte is out of range
Func validIP($ip)
    $ipArray = StringSplit($ip,".")

    If $ipArray[0] <> 4 Then
        Return(1)
    EndIf

    For $X = 1 to 4
        If $ipArray[$X] < 0 OR $ipArray[$X] > 255 Then
            Return(2)
        EndIf
    Next

    Return(0)
EndFunc
Edited by weaponx

Share this post


Link to post
Share on other sites
Moze

Thx weaponx i came with same solution :)

Share this post


Link to post
Share on other sites
enaiman

Minor correction on weaponx script:

The first part of an IP address can't be 0 while any other can be, so the script need to be just a little "tuned" for $ipArray [1] which needs to be >0 and <255


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×