正则验证工具类
发布时间:2020-12-14 06:34:18 所属栏目:百科 来源:网络整理
导读:import java.util.regex.Matcher;import java.util.regex.Pattern;/** * * 正则验证工具类 */public class RegexUtil { /** * 手机号码,中间4位星号替换 * * @param phone 手机号 * @return 星号替换的手机号 string */ public static String phoneNoHide(S
import java.util.regex.Matcher; import java.util.regex.Pattern; /** * * 正则验证工具类 */ public class RegexUtil { /** * 手机号码,中间4位星号替换 * * @param phone 手机号 * @return 星号替换的手机号 string */ public static String phoneNoHide(String phone) { // 括号表示组,被替换的部分$n表示第n组的内容 // 正则表达式中,替换字符串,括号的意思是分组,在replace()方法中, // 参数二中可以使用$n(n为数字)来依次引用模式串中用括号定义的字串。 // "(d{3})d{4}(d{4})","$1****$2"的这个意思就是用括号, // 分为(前3个数字)中间4个数字(最后4个数字)替换为(第一组数值,保持不变$1)(中间为*)(第二组数值,保持不变$2) return phone.replaceAll("(d{3})d{4}(d{4})","$1****$2"); } /** * 银行卡号,保留最后4位,其他星号替换 * * @param cardId 卡号 * @return 星号替换的银行卡号 string */ public static String cardIdHide(String cardId) { return cardId.replaceAll("d{15}(d{3})","**** **** **** **** $1"); } /** * 身份证号,中间10位星号替换 * * @param id 身份证号 * @return 星号替换的身份证号 string */ public static String idHide(String id) { return id.replaceAll("(d{4})d{10}(d{4})","$1** **** ****$2"); } /** * 是否为车牌号(沪A88888) * * @param vehicleNo 车牌号 * @return 是否为车牌号 boolean */ public static boolean checkVehicleNo(String vehicleNo) { Pattern pattern = Pattern.compile("^[u4e00-u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{5}$"); return pattern.matcher(vehicleNo).find(); } /** * 验证身份证号码 * * @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); } /** * 验证手机格式 * 验证成功返回true,验证失败返回false * * @param mobiles the mobiles * @return the boolean */ public static boolean isMobileNO(String mobiles) { /*第一位必定为1,第二位必定为3或4或5或6,7或8,9,其他位置的可以为0-9 */ Pattern p = Pattern.compile("^((1[3,4,5,6,7,8,9][0-9])|(14[5,7])|(17[0,8]))d{8}$"); Matcher m = p.matcher(mobiles); return m.matches(); } /** * 验证固定电话号码 * * @param phone 电话号码,格式:国家(地区)电话代码 + 区号(城市代码) + 电话号码,如:+8602085588447 <p><b>国家(地区) 代码 :</b>标识电话号码的国家(地区)的标准国家(地区)代码。它包含从 0 到 9 的一位或多位数字, 数字之后是空格分隔的国家(地区)代码。</p> <p><b>区号(城市代码):</b>这可能包含一个或多个从 0 到 9 的数字,地区或城市代码放在圆括号—— 对不使用地区或城市代码的国家(地区),则省略该组件。</p> <p><b>电话号码:</b>这包含从 0 到 9 的一个或多个数字 </p> * @return 验证成功返回true ,验证失败返回false */ public static boolean checkPhone(String phone) { String regex = "(+d+)?(d{3,4}-?)?d{7,8}$"; return Pattern.matches(regex,phone); } /** * 验证Email * * @param email email地址,格式:zhangsan@sina.com,zhangsan@xxx.com.cn,xxx代表邮件服务商 * @return 验证成功返回true ,验证失败返回false */ public static boolean checkEmail(String email) { String strPattern = "^s*w+(?:.{0,1}[w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*.[a-zA-Z]+s*$"; Pattern p = Pattern.compile(strPattern); Matcher m = p.matcher(email); return m.matches(); } /** * 昵稱可由中英文,数字,“_”,“-”组成 * * @param nickName the nick name * @return boolean */ public static boolean isNickName(String nickName) { String strPattern = "^[_-a-zA-Z0-9u4e00-u9fa5]+$"; Pattern p = Pattern.compile(strPattern); Matcher m = p.matcher(nickName); return m.matches(); } /** * eos账号名 * * @param eosName the eos name * @return boolean */ public static boolean iSEOsName(String eosName) { String strPattern = "^[a-z]{1}[1-5a-z]{11}$"; Pattern p = Pattern.compile(strPattern); Matcher m = p.matcher(eosName); return m.matches(); } /** * 搜索输入框限制 * * @param eosName the eos name * @return boolean */ public static boolean seachName(String eosName) { String strPattern = "^[.a-z1-5]{7,13}$"; Pattern p = Pattern.compile(strPattern); Matcher m = p.matcher(eosName); return m.matches(); } /** * 密码设置成6-20位,并且由字母,数字和符号两种以上组合 * * @param pwd the pwd * @return boolean */ public static boolean isPwd(String pwd) { String strPattern = "^[wu4e00-u9fa5]{6,12}$"; Pattern p = Pattern.compile(strPattern); Matcher m = p.matcher(pwd); return m.matches(); } /** * 验证输入的名字是否为“中文”或者是否包含“·” * * @param name the name * @return the boolean */ public static boolean isLegalName(String name) { if (name.contains("·") || name.contains("?")) { if (name.matches("^[u4e00-u9fa5]+[·?][u4e00-u9fa5]+$")) { return true; } else { return false; } } else { if (name.matches("^[u4e00-u9fa5]+$")) { return true; } else { return false; } } } /** * 验证整数(正整数和负整数) * * @param digit 一位或多位0-9之间的整数 * @return 验证成功返回true ,验证失败返回false */ public static boolean checkDigit(String digit) { String regex = "-?[1-9]d+"; return Pattern.matches(regex,digit); } /** * 验证整数和浮点数(正负整数和正负浮点数) * * @param decimals 一位或多位0-9之间的浮点数,如:1.23,233.30 * @return 验证成功返回true ,验证失败返回false */ public static boolean checkDecimals(String decimals) { String regex = "-?[1-9]d+(.d+)?"; return Pattern.matches(regex,decimals); } /** * 验证空白字符 * * @param blankSpace 空白字符,包括:空格、t、n、r、f、x0B * @return 验证成功返回true ,验证失败返回false */ public static boolean checkBlankSpace(String blankSpace) { String regex = "s+"; return Pattern.matches(regex,blankSpace); } /** * 验证中文 * * @param chinese 中文字符 * @return 验证成功返回true ,验证失败返回false */ public static boolean checkChinese(String chinese) { String regex = "^[u4E00-u9FA5]+$"; return Pattern.matches(regex,chinese); } /** * 验证日期(年月日) * * @param birthday 日期,格式:1992-09-03,或1992.09.03 * @return 验证成功返回true ,验证失败返回false */ public static boolean checkBirthday(String birthday) { String regex = "[1-9]{4}([-./])d{1,2}1d{1,2}"; return Pattern.matches(regex,birthday); } /** * 验证URL地址 * * @param url * @return 验证成功返回true ,验证失败返回false */ public static boolean checkURL(String url) { String regEx = "^(http|https|ftp)://([a-zA-Z0-9.-]+(:[a-zA-" + "Z0-9.&%$-]+)*@)?((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{" + "2}|[1-9]{1}[0-9]{1}|[1-9]).(25[0-5]|2[0-4][0-9]|[0-1]{1}" + "[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-4][0-9]|" + "[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0).(25[0-5]|2[0-" + "4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0" + "-9-]+.)*[a-zA-Z0-9-]+.[a-zA-Z]{2,4})(:[0-9]+)?(/" + "[^/][a-zA-Z0-9.,?'\/+&%$=~_-@]*)*$"; return Pattern.matches(regEx,url); } /** * 匹配中国邮政编码 * * @param postcode 邮政编码 * @return 验证成功返回true ,验证失败返回false */ public static boolean checkPostcode(String postcode) { String regex = "[1-9]d{5}"; return Pattern.matches(regex,postcode); } /** * 匹配IP地址(简单匹配,格式,如:192.168.1.1,127.0.0.1,没有匹配IP段的大小) * * @param ipAddress IPv4标准地址 * @return 验证成功返回true ,验证失败返回false */ public static boolean checkIpAddress(String ipAddress) { String regex = "[1-9](d{1,2})?.(0|([1-9](d{1,2})?)).(0|([1-9](d{1,2})?))"; return Pattern.matches(regex,ipAddress); } /** * 功能:判断字符串是否为数字 * * @param str * @return */ private static boolean isNumeric(String str) { Pattern pattern = Pattern.compile("[0-9]*"); Matcher isNum = pattern.matcher(str); if (isNum.matches()) { return true; } else { return false; } } /** * 功能:判断字符串是否为日期格式 * * @param strDate the str date * @return boolean */ public static boolean isDate(String strDate) { String regxStr = "^((d{2}(([02468][048])|([13579][26]))[-/s]?((((0?[13578])|(1[02]))[-/s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[-/s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[-/s]?((0?[1-9])|([1-2][0-9])))))|(d{2}(([02468][1235679])|([13579][01345789]))[-/s]?((((0?[13578])|(1[02]))[-/s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[-/s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[-/s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))"; Pattern pattern = Pattern.compile(regxStr); Matcher m = pattern.matcher(strDate); if (m.matches()) { return true; } else { return false; } } /** * 使用java正则表达式去掉多余的.与0 * * @param s * @return */ public static String subZeroAndDot(String s) { if (s.indexOf(".") > 0) { s = s.replaceAll("0+?$","");//去掉多余的0 s = s.replaceAll("[.]$","");//如最后一位是.则去掉 } return s; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |