Jump to content

Question about StringRegExp


Moze
 Share

Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...