In Java, there is InetAdress which can do the validation. The problem is that it requires accessing the network for the dns query. How can I do the validation? There are hostname and ipv4 regex, But I can't find a regex to match ipv6 address.
            Asked
            
        
        
            Active
            
        
            Viewed 538 times
        
    1
            
            
         
    
    
        Abhishek Bhardwaj
        
- 1,164
- 3
- 14
- 39
 
    
    
        zhh
        
- 2,346
- 1
- 11
- 22
- 
                    1Look here: https://stackoverflow.com/questions/9208814/validate-ipv4-ipv6-and-hostname and here https://stackoverflow.com/questions/3114595/java-regex-for-accepting-a-valid-hostname-ipv4-or-ipv6-address – Nurlan Nabiyev Oct 27 '17 at 06:08
- 
                    @Nurlan Nabiyev I'll checkout InetAdressValidator – zhh Oct 27 '17 at 06:14
1 Answers
0
            
            
        The IPAddress Java library can parse host names, IPv4 and IPv6 addresses, without triggering DNS lookup. Disclaimer: I am the project manager of that library.
Sample code:
HostName host = new HostName(hostStr);
try {
    host.validate();
    if(host.isAddress()) {
        System.out.println("address: " + host.asAddress());
    } else {
        System.out.println("host name: " + host);
    }
} catch(HostNameException e) {
    System.out.println(e.getMessage());
}
This will not do DNS lookup. More sample code at the wiki
 
    
    
        Sean F
        
- 4,344
- 16
- 30