(2.3.12.1)正则校验字符串是否为数字

前端之家收集整理的这篇文章主要介绍了(2.3.12.1)正则校验字符串是否为数字前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.sangfor.pocket.expenses.util;

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

/** * 类描述: * <p/> * Created by sf on 2016/5/5. */
public class NumberValidationUtils {

    private static boolean isMatch(String regex,String orginal){
        if (orginal == null || orginal.trim().equals("")) {
            return false;
        }
        Pattern pattern = Pattern.compile(regex);
        Matcher isNum = pattern.matcher(orginal);
        return isNum.matches();
    }

    public static boolean isPositiveInteger(String orginal) {
        return isMatch("^\\+{0,1}[1-9]\\d*",orginal);
    }

    public static boolean isNegativeInteger(String orginal) {
        return isMatch("^-[1-9]\\d*",orginal);
    }

    public static boolean isWholeNumber(String orginal) {
        return isMatch("[+-]{0,1}0",orginal) || isPositiveInteger(orginal) || isNegativeInteger(orginal);
    }

    public static boolean isPositiveDecimal(String orginal){
        return isMatch("\\+{0,1}[0]\\.[1-9]*|\\+{0,1}[1-9]\\d*\\.\\d*",orginal);
    }

    public static boolean isNegativeDecimal(String orginal){
        return isMatch("^-[0]\\.[1-9]*|^-[1-9]\\d*\\.\\d*",orginal);
    }

    public static boolean isDecimal(String orginal){
        return isMatch("[-+]{0,1}\\d+\\.\\d*|[-+]{0,1}\\d*\\.\\d+",orginal);
    }

    public static boolean isRealNumber(String orginal){
        return isWholeNumber(orginal) || isDecimal(orginal);
    }

    //最高9位整数,最高2位小数
    public static final String AMOUNT_ZERO_REGEX = "(0*)(\\.0{0,2})?"; //0正则
    public static final String AMOUNT_DECIMAL_REGEX = "(\\d*)(\\.\\d{0,2})?"; //正小数正则
    public static final String NUMBER_REGEX = "^[1-9]+\\d*$";// 必须是1-9开头的。
    public static final String USER_NAME_REGEX = "^[\\w\\u4e00-\\u9fa5][\\s\\w\\u4e00-\\u9fa5]*(?!\\s)$"; //名字过滤正则
    public static boolean isMax92Decimal(String orginal){
        return isMatch("^([1-9]\\d{0,8})(\\.\\d{0,2})?$",orginal);
    }

}
原文链接:https://www.f2er.com/regex/359268.html

猜你在找的正则表达式相关文章