java 验证系列

慢慢完善

一.验证IP

 /**
     * 正则表达式判断IP
     * @param ip
     * @return
     */
    public static boolean checkIP(String ip) {  
        String rexp = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."  
                +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."  
                +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."  
                +"(00?\\d|1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";  
          
        Pattern pat = Pattern.compile(rexp);    
        Matcher mat = pat.matcher(ip);    
        boolean isIP = mat.find();
        return isIP;
    }

二.验证port

 /**
     * 正则表达式判断端口
     * @param port
     * @return
     */
    public static boolean checkPort(String port) {
        //端口号验证 1 ~ 65535
        String regex = "^([0-9]|[1-9]\\d|[1-9]\\d{2}|[1-9]\\d{3}|[1-5]\\d{4}|6[0-4]\\d{3}|65[0-4]\\d{2}|655[0-2]\\d|6553[0-5])$";
        boolean isPort = Pattern.matches(regex, port);
        return isPort;
    }

猜你喜欢

转载自www.cnblogs.com/JoeyWong/p/9077246.html