JAVA使用正则表达式对数值进行限制

JAVA使用正则表达式对数值进行限制

web开发中对数值进行约束,可以在页面处理,也可以在后端处理。为了保险,两边都进行约束。我记录一下后端Java的处理方便以后查看。

public class RegexUtils {
	/**
	 * 匹配 最大10位  小数最多两位
	 * @param content
	 * @return
	 */
	public static boolean Max9dec2(String content){
		String regex ="[0]|[1-9][0-9]{0,8}|[0]\\.[0-9]{1,2}|[1-9][0-9]{0,7}\\.[0-9]{1}|[1-9][0-9]{0,6}\\.[0-9]{2}";
		boolean ismatch = Pattern.matches(regex, content);
		return ismatch;
	}
	/**
	 * 匹配最大10位 非零
	 * @param content
	 * @return
	 */
	public static boolean Max10(String content){
		String regex ="[1-9][0-9]{0,9}";
		boolean ismatch = Pattern.matches(regex, content);
		return ismatch;
	}
	/**
	 * 
	 * @param content
	 * @return
	 */
	public static boolean Max9(String content) {
		String regex ="[1-9][0-9]{0,8}";
		boolean ismatch = Pattern.matches(regex, content);
		return ismatch;
	}
	
	/**
	 * 匹配最大2位 非零
	 * @param content
	 * @return
	 */
	public static boolean Max2(String content){
		String regex ="[1-9][0-9]{0,1}";
		boolean ismatch = Pattern.matches(regex, content);
		return ismatch;
	}
	/**
	 * 匹配 最大5位  小数最多两位
	 * @param content
	 * @return
	 */
	public static boolean Max5dec2(String content){
		String regex ="[0]|[1-9][0-9]{0,4}|[0]\\.[0-9]{1,2}|[1-9][0-9]{0,3}\\.[0-9]{1}|[1-9][0-9]{0,2}\\.[0-9]{2}";
		boolean ismatch = Pattern.matches(regex, content);
		return ismatch;
	}

	/**
	 * 匹配小于0 最多两位小数
	 * @param content
	 * @return
	 */
	public static boolean less1dec2(String content){
		boolean ismatch = true;
		String regex = "[0]\\.[0-9][0-9]{0,1}";
		ismatch = Pattern.matches(regex, content);
		return ismatch;
	}
	
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值