8

From a given String:

String someIp = // some String

How can I check, if someIp is a valid Ip format?

Michael
  • 32,527
  • 49
  • 210
  • 370

4 Answers4

13

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..,.

MKB
  • 7,587
  • 9
  • 45
  • 71
3

Regexes will do. There are simple ones and more complex. A simple one is this regex:

\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}

Use it like this:

boolean isIP = someIP.maches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");

But this will match 999.999.999.999 as well, which is not a valid IP address. There is a huge regex available on regular-expressions.info:

(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)

This one will take care of the job correctly. If you use this one, don't forget to escape every \ with another \.


If you are not a fan of huge regexes, you can use this code:

public static boolean isIP(String str)
{
    try
    {
         String[] parts = str.split("\\.");
         if (parts.length != 4) return false;
         for (int i = 0; i < 4; ++i)
         {
             int p = Integer.parseInt(parts[i]);
             if (p > 255 || p < 0) return false;
         }
         return true;
    } catch (Exception e)
    {
        return false;
    }
}
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • 2
    This is the superior answer, and he clearly knows REGEX in java better than I do, go with this one. I'm fairly new to the world of REGEX, and haven't used it in JAVA much yet. – CamelopardalisRex Aug 10 '13 at 00:00
  • Very good. Also, in Groovy, you can use `/regex/` string, thus not needing escapes, and use `==~` operator to check the regex match – Will Aug 10 '13 at 01:13
1

An oriented object way:

String myIp ="192.168.43.32"
myIp.isIp();

Known that , you must add this to BootStrap.groovy:

String.metaClass.isIp={
   if(org.codehaus.groovy.grails.validation.routines.InetAddressValidator.getInstance().isValidInet4Address(delegate)){
    return true;
   } else {
    return false;
    } 


}
Abdennour TOUMI
  • 87,526
  • 38
  • 249
  • 254
0

Use a validator like Apache's commons-validator:

@Grab('commons-validator:commons-validator:1.7')
import org.apache.commons.validator.routines.InetAddressValidator

boolean isIpV4(String ip) {
    InetAddressValidator.instance.isValidInet4Address(ip)
}

isIpV4("8.8.8.8")

Or regex:

import java.util.regex.Pattern

class RegexValidationConstants {
    private final static String IPV4_OCT = /(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/
    final static Pattern IPV4 = ~/^${IPV4_OCT}(\.${IPV4_OCT}){3}$/.toString()
}

boolean isIpV4(String ip) {
    ip ==~ RegexValidationConstants.IPV4
}

isIpV4("8.8.8.8")

If you are using Grails, see my answer in this other thread.

Maicon Mauricio
  • 2,052
  • 1
  • 13
  • 29