package com.org.hktshop.utils;

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

public class StringValidatorUtils {
    /**
     * @param regex
     *            正则表达式字符串
     * @param str
     *            要匹配的字符串
     * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
     */
    private static boolean match(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    public static boolean isNum(String str) {
        String regex = "[0-9.]+";
        return match(regex, str);
    }

    public static boolean isIntegerNum(String str) {
        String regex = "[0-9]+";
        return match(regex, str);
    }

    public static boolean isInteger(String str) {
        try {
            Integer.valueOf(str);
        } catch (NumberFormatException e) {
            return false;
        }
        return true;
    }

    public static boolean isLong(String str) {
        try {
            Long.valueOf(str);
        } catch (NumberFormatException e) {
            return false;
        }
        return true;
    }
}
