Java 常用的正则表达式

最近做功能遇到很多正则校验, 分享一下

package com.souche.md.util;

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

public class RegexUtils {

	/**
	 * 验证Email
	 * 
	 * @param email email地址,格式:zhangsan@zuidaima.com,zhangsan@xxx.com.cn,xxx代表邮件服务商
	 * @return 验证成功返回true,验证失败返回false
	 */
	public static boolean checkEmail(String email) {
		String regex = "\\w+@\\w+\\.[a-z]+(\\.[a-z]+)?";
		return Pattern.matches(regex, email);
	}

	/**
	 * 验证身份证号码
	 * 
	 * @param idCard 居民身份证号码15位或18位,最后一位可能是数字或字母
	 * @return 验证成功返回true,验证失败返回false
	 */
	public static boolean checkIdCard(String idCard) {
		String regex = "[1-9]\\d{13,16}[a-zA-Z0-9]{1}";
		return Pattern.matches(regex, idCard);
	}

	/**
	 * 验证整数
	 * 
	 * @param digit 一位或多位0-9之间的整数
	 * @return 验证成功返回true,验证失败返回false
	 */
	public static boolean checkDigit(String digit) {
		String regex = "^\\d+$";
		return Pattern.matches(regex, digit);
	}

	/**
	 * 非负数
	 * 
	 * @param digit
	 * @return
	 */
	public static boolean nonnegativeNumber(String digit) {
		String regex = "^\\d+(\\.\\d+)?$";
		return Pattern.matches(regex, digit);
	}

	/**
	 * 判断特殊字符
	 * 
	 * @param str
	 * @return
	 */
	public static String FilterStr(String str) {
		/**
		 * 特殊字符
		 */
		String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?_]";

		Pattern pat = Pattern.compile(regEx);
		Matcher mat = pat.matcher(str);
		/**
		 * 返回替换结果
		 */
		return mat.replaceAll("").trim();
	}

	/**
	 * 校验字符串重是否存在特殊字符
	 * 
	 * @param str
	 * @return
	 */
	public static boolean checkCharacter(String str) {
		/**
		 * 特殊字符
		 */
		String regEx = "[`~!@#¥$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";

		Pattern pat = Pattern.compile(regEx);
		Matcher mat = pat.matcher(str);

		return mat.find();
	}




	/**
	 * 校验字符串中是否存在空格
	 * 
	 * @param str
	 * @return
	 */
	public static boolean checkeSpace(String str) {
		if (str.indexOf(" ") != -1) {
			return true;
		}
		return false;
	}
	/**
	 * 金额
	 * @param value
	 * @return
	 */
	public static boolean isMoney(String value) {
		String regexValue = "^(0|[1-9][0-9]{0,11})(\\.[0-9]{1,2})?$";
		boolean regexResult = value.matches(regexValue);
		return regexResult;
	}

	/**
	 * 金额
	 * @param value  整数最多9位;小数最多两位
	 * @return
	 */
	public static boolean isMoneyDigitEleven(String value) {
		String regexValue = "^(0|[1-9][0-9]{0,8})(\\.[0-9]{1,2})?$";
		boolean regexResult = value.matches(regexValue);
		return regexResult;
	}

	/**
	 * @params String value
	 * @return
	 * @description 校验(20字符以内;除特殊符号;)
	 */
	public static Boolean checkSpecialCharacters(String value) {
		// 正则表达式:20字符以内;除特殊符号;
		String regexValue = "^[a-zA-Z0-9\u4e00-\u9fa5]{1,20}$";
		boolean regexResult = value.matches(regexValue);
		return regexResult;
	}

	public static Boolean isProportion(String value) {
		// 正则表达式:20字符以内;除特殊符号;
		String regexValue = "^(100|[1-9]\\d|\\d)(.\\d{1,2})?$";
		boolean regexResult = value.matches(regexValue);
		return regexResult;
	}

	public static boolean isChineseWord(String value){

		String regexValue = "[\u4e00-\u9fa5]+";

		boolean regexResult = value.matches(regexValue);

		return regexResult;

	}
	/**
	* @Description 字符串是否是字母或数字
	* @date 2019/5/14
	* @param [str]
	* @return boolean
	* @author xrl
	*/
	public static boolean isWordOrNum(String str){
		String regEx="[A-Z,a-z,0-9,-]*";
		Pattern pattern = Pattern.compile(regEx);
		return pattern.matcher(str).matches();
	}

//	public static void main(String[] args) {
//
//		boolean a = nonnegativeNumber("dddd你好");
//
//		System.out.println(a);
//
//		boolean b = nonnegativeNumber("@1");
//		System.out.println(b);
//		boolean c = nonnegativeNumber("-11");
//
//		System.out.println(c);
//
//	}

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值