Here you can find the source of isIPAddress(final String s)
Parameter | Description |
---|---|
s | to match |
public static boolean isIPAddress(final String s)
//package com.java2s; /* See LICENSE for licensing and NOTICE for copyright. */ import java.util.regex.Pattern; public class Main { /** Pattern to match ipv4 addresses. */ private static final Pattern IPV4_PATTERN = Pattern .compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)" + "(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$"); /** Pattern to match ipv6 addresses. */ private static final Pattern IPV6_STD_PATTERN = Pattern.compile("^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$"); /** Pattern to match ipv6 hex compressed addresses. */ private static final Pattern IPV6_HEX_COMPRESSED_PATTERN = Pattern.compile( "^((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::" + "((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)$"); /**// w ww . j av a 2 s . com * Returns whether the supplied string represents an IP address. Matches both IPv4 and IPv6 addresses. * * @param s to match * * @return whether the supplied string represents an IP address */ public static boolean isIPAddress(final String s) { return s != null && (IPV4_PATTERN.matcher(s).matches() || IPV6_STD_PATTERN.matcher(s).matches() || IPV6_HEX_COMPRESSED_PATTERN.matcher(s).matches()); } }