这篇文章主要介绍了java实现的正则工具类,可用于针对电话号码、邮箱、QQ号码、QQ密码、手机号的正则验证功能,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了java实现的正则工具类。分享给大家供大家参考。具体如下:
这里实现的正则工具类适用于:正则电话号码、邮箱、QQ号码、QQ密码、手机号
java代码如下:
- package com.zhanggeng.contact.tools;
- /**
- * RegexTool is used to regex the string ,such as : phone , qq , password , email .
- *
- * @author ZHANGGeng
- * @version v1.0.1
- * @since JDK5.0
- *
- */
- public class RegexTool {
- /**
- *
- * @param phoneNum 传入的参数仅仅是一个电话号码时,调用此方法
- * @return 如果匹配正确,return true , else return else
- */
- //如果传进来的是电话号码,则对电话号码进行正则匹配
- public static boolean regexPhoneNumber(String phoneNum){
- //电话号码匹配结果
- boolean isPhoneNum_matcher = phoneNum.matches("1[358]//d{9}");
- //如果isPhoneNum_matcher is true , 则return true , else return false
- if(isPhoneNum_matcher)
- return true;
- return false;
- }
- /**
- *
- * @param email 传入的参数仅仅是一个邮箱地址时,调用此方法
- * @return 如果匹配正确,return true , else return false
- */
- //如果传进来的是邮箱地址,则对邮箱进行正则匹配
- public static boolean regexEmailAddress(String email){
- //邮箱匹配结果
- boolean isEmail_matcher = email.matches("[a-zA-Z_0-9]+@[a-zA-Z0-9]+(//.[a-zA-Z]{2,}){1,3}");
- //如果isEmail_matcher value is true , 则 return true , else return false
- if(isEmail_matcher)
- return true;
- return false;
- }
- /**
- *
- * @param phoneNum 传入的电话号码
- * @param email 传入的邮箱地址
- * @return 如果匹配正确,return true , else return false
- */
- public static boolean regexEmailAddressAndPhoneNum(String phoneNum , String email){
- //电话号码匹配结果
- boolean isPhoneNum_matcher = phoneNum.matches("1[358]//d{9}");
- //邮箱匹配结果
- boolean isEmail_matcher = email.matches("[a-zA-Z_0-9]+@[a-zA-Z0-9]+(//.[a-zA-Z]{2,}){1,3}");
- //matcher value is true , 则 return true , else return false
- if(isEmail_matcher && isPhoneNum_matcher){
- return true;
- }
- return false;
- }
- /**
- *
- * @param qqNum 传入的QQ
- * @return 如果匹配正确,return true, else return false
- */
- public static boolean regexQQNumber(String qqNum){
- //QQ号匹配结果
- boolean isQQNum_matcher = qqNum.matches("[1-9]//d{2,11}");
- if(isQQNum_matcher)
- return true;
- return false;
- }
- /**
- *
- * @param pwd 传入的是 密码
- * @return 如果匹配正确,满足密码规则,return true, else return false
- */
- public static boolean regexPassWord(String pwd){
- //密码匹配结果
- boolean isPassWord_matcher = pwd.matches("[0-9a-zA-Z_@$@]{6,12}");
- if(isPassWord_matcher)
- return true;
- return false;
- }
- }
希望本文所述对大家的java程序设计有所帮助。
新闻热点
疑难解答
图片精选