Java IP Address Validate isIpAddress(String ipAddress)

Here you can find the source of isIpAddress(String ipAddress)

Description

Determine if the given string is a valid IPv4 or IPv6 address.

License

Apache License

Parameter

Parameter Description
ipAddress A string that is to be examined to verify whether or not it could be a valid IP address.

Return

true if the string is a value that is a valid IP address, false otherwise.

Declaration

public static boolean isIpAddress(String ipAddress) 

Method Source Code

//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();
    }
}

Related

  1. isIP4(String val)
  2. isIPAddress(final String s)
  3. isIpAddress(String addr)
  4. isIpAddress(String ip)
  5. isIPAddress(String ipAddress)
  6. isIpAddress(String ipAddress)
  7. isIPAddress(String ipAddress)
  8. isIPAddress(String line)
  9. isIpAddress(String str)