Java tutorial
//package com.java2s; //License from project: Apache License import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static boolean isValidIpAddress(String input) { String ipv4Regex = "(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])"; String ipv6Regex = "([0-9a-f]{1,4}:){7}([0-9a-f]){1,4}"; Pattern ipv4Pattern = Pattern.compile(ipv4Regex); Pattern ipv6Pattern = Pattern.compile(ipv6Regex); Matcher ipv4Matcher = ipv4Pattern.matcher(input); if (ipv4Matcher.matches()) return true; Matcher ipv6Matcher = ipv6Pattern.matcher(input); return ipv6Matcher.matches(); } }