0

Assume a java string that contains an IP (v4 or v6) or a hostname.
What is the best way to detect among these cases?
I am not interested so much on whether the IP is in valid range (e.g. 999.999.999.999 for IPv4).
I am interested in just a way to detect if a String is a hostname or an IP (v4 or v6).
Initially I though this:

if(theString.indexOf("@")!=-1){
//Not an Ip
}

but I am not sure if I should always expect a format containing @ always.

Thanks

Cratylus
  • 52,998
  • 69
  • 209
  • 339
  • You're looking for an '@' in a hostname? I posted an answer, but I'm unsure why you're concerned with that character. – A. R. Younce Mar 03 '11 at 07:23
  • Isn't somename@domainName a valid hostname? – Cratylus Mar 03 '11 at 07:33
  • That could be an email address. A hostname refers to a name that when queried in the DNS could potentially be resolved in some way. See more here: http://en.wikipedia.org/wiki/Hostname – A. R. Younce Mar 03 '11 at 08:39

3 Answers3

2

A simple set of regular expressions with the Pattern class will work for this:

Pattern validIpAddressRegex = Pattern.compile("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
Pattern validHostnameRegex = Pattern.compile("^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$");

// matches(String) Will return a boolean value
validIpAddress.matches("111.222.333.444"); 

I borrowed the regular expressions from this SO answer. Check the Wikipedia article on hostnames for more on what constitutes a valid hostname.

Community
  • 1
  • 1
A. R. Younce
  • 1,913
  • 17
  • 22
1

If your address has colons : then it is IPv6 address. Rest of chars can be hex digits (0-1, A-F, a-f).

If address has only digits and dots then it is IPv4 address.

If address has any letter then it is hostname (hostname can have digits, dots and other chars as well).

This can be simply implemented as:

public static String kindOfIP(final String addr)
    {
    if (addr.indexOf(":") >= 0)
        return "IPv6";
    if (addr.matches("^\\d+\\.\\d+\\.\\d+\\.\\d+$"))
        return "IPv4";
    return "hostname";
    }

This code do not check if address is valid.

Michał Niklas
  • 53,067
  • 18
  • 70
  • 114
  • Hostname can have letter, digit or hyphen (some use underscore but it is better not to use it). Dots are part of DNS (you can have name like: hostname.domain or stackoverflow.com). Have a look at: http://en.wikipedia.org/wiki/Hostname#Restrictions_on_valid_host_names – Michał Niklas Mar 03 '11 at 08:19
  • If you see colons in browser URL address then it usually specify non standard port of service. Example: `http://localhost:8080/`. If your strings can have such ports then to check if address is IPv6 you can count how many `:` is in address. If more than 1 then it is IPv6. If only one it may separate hostname and port. – Michał Niklas Mar 03 '11 at 08:23
0

Use regular expression to match vs ip, if its not ip then its a hostname.

Community
  • 1
  • 1
m0s
  • 4,250
  • 9
  • 41
  • 64