From a given String:
String someIp = // some String
How can I check, if someIp is a valid Ip format?
An IP address is a string of numbers separated by periods. IP addresses are expressed as a set of four numbers — an example address might be 192.158.1.38. Each number in the set can range from 0 to 255. So, the full IP addressing range goes from 0.0.0.0 to 255.255.255.255.
This requires a call to InetAddress. getByName() in the Java API. If the URL already has the address then InetAddress. getByName() will simply return the address as-is.
// this is the regex to validate an IP address. = zeroTo255 + "\\." + zeroTo255 + "\\."
Groovy regular expressions have a ==~ operator which will determine if your string matches a given regular expression pattern.
You can use InetAddressValidator class to check and validate weather a string is a valid ip or not.
import org.codehaus.groovy.grails.validation.routines.InetAddressValidator
...
String someIp = // some String
if(InetAddressValidator.getInstance().isValidInet4Address(someIp)){
println "Valid Ip"
} else {
println "Invalid Ip"
}
...
Try this..,.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With