package com.casic.missiles.modular.system.util;

import org.apache.commons.lang.StringUtils;

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

public class RegUtil {


    public static boolean isDecimal2(String str) {// 判断小数点后2位的数字的正则表达式

        Pattern pattern = Pattern.compile("^\\\\d{0,8}\\\\.{0,1}(\\\\d{1,2})?$");
        Matcher match = pattern.matcher(str);
        return match.matches();

    }

    public static boolean isNumOrLetter(String str) {// 判断字母或数字
        return match(str,"^\\w+$");
    }

    public final static boolean isMobile(String text){
        if(text.length()!=11) return false;
        return match(text, "^(((1+\\d{10})$");
    }



    public static boolean isIp(String str) {// 判断iP
        if(StringUtils.isBlank(str)){
            return true;
        }

        Pattern pattern = Pattern.compile("([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}");
        Matcher match = pattern.matcher(str);
        return match.matches();

    }

    public static boolean isPort(String str) {// 判断iP
        if(StringUtils.isBlank(str)){
            return true;
        }
        Pattern pattern = Pattern.compile("^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]{1}|6553[0-5])$");
        Matcher match = pattern.matcher(str);
        return match.matches();

    }


    public static boolean isInteger(String str) {// 判断iP
        return match(str,"^[1-9]\\d*$");
    }

    private final static boolean match(String text, String reg) {
        if (StringUtils.isBlank(text) || StringUtils.isBlank(reg))
            return false;
        return Pattern.compile(reg).matcher(text).matches();
    }


}
