最近做功能遇到很多正则校验, 分享一下
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);
//
// }
}