Sign in to follow this  
Followers 0
Raestlin

Stringregexp Help Please...

4 posts in this topic

I would like to check an input string to verify whether someone input a valid IP address:

For example: ###.###.###.### or ##.##.##.##, etc.

Has anyone done this yet?

Can "StringRegExp" be used?

Is there a better way?

Thank you in advance for your help!

~ Chris (Taz)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

This is close, but I can't figure out how to look for nothing.

(([:digit:]{1,3}[.]{1}){3}([:digit:]{1,3}))
Edited by big_daddy

Share this post


Link to post
Share on other sites

This is close, but I can't figure out how to look for nothing.

(([:digit:]{1,3}[.]{1}){3}([:digit:]{1,3}))

I'll try that thanks...

In the meantime I just wrote a little function:

; Function to validate whether an input is in a valid IP address notation
Func ValidateIP($ipaddr)
   Local $validip, $dummy, $d1
   $validip = 0
   $dummy = StringSplit($ipaddr, ".")
   If $dummy[0] = 4 Then; String at least has 4 octets
      If (Int($dummy[1]) > 0) And (Int($dummy[1]) < 256) Then; Check 1st octet = 1-255
         For $d1 = 2 to 4
            If (Int($dummy[$d1]) > -1) And (Int($dummy[$d1]) < 256) Then; Check 2nd, 3rd, 4th octet = 0-255
               $validip = 1
            EndIf
         Next
      EndIf
   EndIf
   If $validip Then
      Return 1
   Else
      MsgBox(48,"Invalid IP","The IP address you entered is not in a valid format. Please check it and try again.")
      Return 0
   EndIf
EndFunc

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

$ip ="127.0.0.1"
  $isip = StringRegExp ( $ip, "[12]?[0-9]{1,2}\.[12]?[0-9]{1,2}\.[12]?[0-9]{1,2}\.[12]?[0-9]{1,2}",0)

that might work but is not fool proof (oops there was a stupid typo here)

The other example to check every number for its maximum

This could also be done in some way with stringregexp (seen it on some website)

But this does the same.

$ip = "127.0.0.1"

$isip = StringRegExp ( $ip, "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}",0) 

if $isip then
    $array1 =  stringsplit($ip,'.')
    $ipcheck = 0
    For $count1 = 1 to $array1[0]
        if int($array1[$count1]) > 255 then $ipcheck += 1
    Next

    if $ipcheck < 1 then
        msgbox(0,"","Valid ip address ")
    else
        msgbox(0,"","Invalid ip address(not 4 bytes in size)")
    endif
else
    msgbox(0,"","Invalid string")
endif

There could also be a way to convert the ip to one binary and do some bit comparison I tried that but got confused (too tired or lazy) This is actually a better way on wich you could also use subnetmasks etc but too bad I am bad

Edited by MrSpacely

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  
Followers 0