test if a given string is an ip a address

hi everyone! i want to test if a given string is an ip a address. how could i do this using desktop app? -->will be correct
192.168.1. -->error -->error -->error
ds.168.1.0 --> error

thanks a lot!

Use Regular Expressions. You can find examples of RegEx patterns for IP address here.

Here is an implementation to check for IP v4:

Function IsIPv4(s As String) As Boolean
  Dim rg as New RegEx
  Dim match as RegExMatch
  Dim result As Boolean
  rg.SearchPattern = "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
  match = rg.search(s)
  result = (match <> Nil)
  return result
End Function

…and to use…

  if IsIPv4("") then
    MsgBox "Ping me!"
  end if

In the RegExRX samples, I include four different patterns for this. Two of them are variations on what Alwyn posted above, one will exclude unusable addresses, and the last will exclude unusables and loopbacks (127.x.x.x). Let me know if you’d like either of those.

1 Like

wow! great responses in record time! i didn’t know regex. kem and eric helped me to understand and alwyn helped me with the hands on. thanks a lot again. my question is solved.

After testing the formal validity of the address, you may want to check it has an active connection.

See https://forum.xojo.com/10912-pass-string-to-shell-command

Should be:

rg.SearchPattern = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
1 Like