Here you can find the source of isIpAddress(String ipAddress)
Parameter | Description |
---|---|
ipAddress | A string that is to be examined to verify whether or not it could be a valid IP address. |
true
if the string is a value that is a valid IP address, false
otherwise.
public static boolean isIpAddress(String ipAddress)
//package com.java2s; /*// w w w. ja v a 2 s . c o m * * Copyright (c) 2015 University of Massachusetts * * Licensed under the Apache License, Version 2.0 (the "License"); you * may not use this file except in compliance with the License. You * may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * implied. See the License for the specific language governing * permissions and limitations under the License. * * Initial developer(s): Westy * */ import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { private static Pattern VALID_IPV4_PATTERN = null; private static Pattern VALID_IPV6_PATTERN = null; /** * Determine if the given string is a valid IPv4 or IPv6 address. This method * uses pattern matching to see if the given string could be a valid IP address. * * @param ipAddress A string that is to be examined to verify whether or not * it could be a valid IP address. * @return <code>true</code> if the string is a value that is a valid IP address, * <code>false</code> otherwise. */ public static boolean isIpAddress(String ipAddress) { Matcher m1 = VALID_IPV4_PATTERN.matcher(ipAddress); if (m1.matches()) { return true; } Matcher m2 = VALID_IPV6_PATTERN.matcher(ipAddress); return m2.matches(); } }