Java IP Address Validate isIP(String input)

Here you can find the source of isIP(String input)

Description

Checks if a given string is an IP address.

License

Apache License

Parameter

Parameter Description
input a parameter

Declaration

public static boolean isIP(String input) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**//  w ww. j a va 2 s.c  om
     * Checks if a given string is an IP address.
     * 
     * @param input
     * @return
     */
    public static boolean isIP(String input) {
        String regex = "(?:[0-9]{1,3}\\.){3}[0-9]{1,3}";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(input);
        String ss = input.replace(".", "");
        if (m.find() && isNumeric(ss) && (input.length() - ss.length() > 2)) {
            return true;
        }
        return false;
    }

    /**
     * Replaces strings with old references with ones with updated references.
     * 
     * @param orig
     * @param oldStr
     * @param newStr
     * @return
     */
    public static String replace(String orig, String oldStr, String newStr) {
        StringBuffer sb = new StringBuffer(orig);
        while (contains(sb.toString(), oldStr)) {
            // System.out.println(sb.toString() + ":" + oldStr + ":" + newStr);
            if (orig.contains("(") && orig.contains(";")) {
                // orig is most likely a method desc
                int start = sb.indexOf("L" + oldStr) + 1;
                int end = sb.indexOf(oldStr + ";") + oldStr.length();
                if (start > -1 && end <= orig.length()) {
                    sb.replace(start, end, newStr);
                } else {
                    System.err.println("REPLACE FAIL: (" + oldStr + ") - " + orig);
                    break;
                }
            } else if (orig.startsWith("L") && orig.endsWith(";")) {
                // orig is most likely a class desc
                if (orig.substring(1, orig.length() - 1).equals(oldStr)) {
                    sb.replace(1, orig.length() - 1, newStr);
                }
            } else {
                // Dunno
                if (orig.equals(oldStr)) {
                    sb.replace(0, sb.length(), newStr);
                } else {
                    // This shouldn't happen.
                    System.err.println("FUCK: (" + sb.toString() + ") - " + oldStr + ":" + newStr);
                    break;
                }
            }
        }
        return sb.toString();
    }

    /**
     * Checks if a string is a number
     * 
     * @param s
     * @return
     */
    public static boolean isNumeric(String s) {
        try {
            Double.parseDouble(s);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean contains(String orig, String check) {
        if (orig.contains(check)) {
            String regex = "([L]" + check.replace("/", "\\/") + "{1}[;])";
            if (orig.contains("(") && orig.contains(";")) {
                return orig.matches(regex);
            } else if (orig.startsWith("L") && orig.endsWith(";")
                    && orig.substring(1, orig.length() - 1).equals(check)) {
                return true;
            } else if (orig.equals(check)) {
                return true;
            }
        }
        return false;
    }
}

Related

  1. isIp(final String input)
  2. isIp(Object obj)
  3. isIp(String ip)
  4. isIP(String ip)
  5. isIp(String ipAddress)
  6. isIP(String ipString)