java 代码实现各数据的正则校验
最近公司的項目,關(guān)于excel導(dǎo)入導(dǎo)出,對數(shù)據(jù)要進(jìn)行校驗,所以就對數(shù)據(jù)的正則校驗就用的比較多,自己也是一點點去查,現(xiàn)在項目完成了,就把所有用到的都做一個小結(jié),或許以后還可以用到!
package org.asyware.insurance.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
?*?
?* @author zl
?*?
?*
?*/
public class DataCheckUtil {
?/**
? ? ?* @see 驗證手機(jī)號是否合法
? ? ?* @param mobiles
? ? ?* @return ?合法:true ?不合法:false
? ? ?*/
? ? public static boolean isMobileNO(String mobile){ ?
? ? ? ? if (mobile.length() != 11)
? ? ? ? {
? ? ? ? ? ? return false;
? ? ? ? }else{
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 移動號段正則表達(dá)式
? ? ? ? ? ? ?*?
? ? ? ? ? ? ?*/
? ? ? ? ? ? String pat1 = "^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 聯(lián)通號段正則表達(dá)式
? ? ? ? ? ? ?*/
? ? ? ? ? ? String pat2 ?= "^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 電信號段正則表達(dá)式
? ? ? ? ? ? ?*/
? ? ? ? ? ? String pat3 ?= "^((133)|(153)|(177)|(18[0,1,9])|(149)|(199))\\d{8}$";
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 虛擬運營商正則表達(dá)式
? ? ? ? ? ? ?*/
? ? ? ? ? ? String pat4 = "^((170))\\d{8}|(1718)|(1719)\\d{7}$";
? ? ? ? ? ? Pattern pattern1 = Pattern.compile(pat1);
? ? ? ? ? ? Matcher match1 = pattern1.matcher(mobile);
? ? ? ? ? ? boolean isMatch1 = match1.matches();
? ? ? ? ? ? if(isMatch1){
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? ? ? Pattern pattern2 = Pattern.compile(pat2);
? ? ? ? ? ? Matcher match2 = pattern2.matcher(mobile);
? ? ? ? ? ? boolean isMatch2 = match2.matches();
? ? ? ? ? ? if(isMatch2){
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? ? ? Pattern pattern3 = Pattern.compile(pat3);
? ? ? ? ? ? Matcher match3 = pattern3.matcher(mobile);
? ? ? ? ? ? boolean isMatch3 = match3.matches();
? ? ? ? ? ? if(isMatch3){
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? ? ? Pattern pattern4 = Pattern.compile(pat4);
? ? ? ? ? ? Matcher match4 = pattern4.matcher(mobile);
? ? ? ? ? ? boolean isMatch4 = match4.matches();
? ? ? ? ? ? if(isMatch4){
? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? }
? ? ? ? ? ? return false;?
? ? ? ? }
? ? }?
?? ?
?? ?
?? ?/**
?? ? * 判斷導(dǎo)入的數(shù)據(jù)是全中文
?? ? * @param str
?? ? * @return
?? ? */
?? ?public static boolean ?isChinese(String str) {
?? ??? ?String regEx = "[\u4e00-\u9fa5]";
?? ??? ?Pattern pat = Pattern.compile(regEx);
?? ??? ?Matcher matcher = pat.matcher(str);
?? ??? ?if (matcher.find()) {
?? ??? ??? ?return true;
?? ??? ?}else {
?? ??? ??? ?return false;
?? ??? ?}??
?? ?}
?? ?
?? ?/**
?? ? * 卡號校驗是否是純數(shù)字
?? ? * @param args
?? ? * @return
?? ? */
?? ?public static boolean isNum(String str){
//?? ??? ?String str1="[0-9]{1,}";?? ??? ?
//?? ??? ?Pattern pat = Pattern.compile(str1);
//?? ??? ?Matcher matcher = pat.matcher((CharSequence)str);
//?? ??? ?if(matcher.matches()) {
//?? ??? ??? ?return true;
//?? ??? ?}else {
//?? ??? ??? ?return false;
//?? ??? ?}
?? ??? ?String regex = "^[0-9]*$";
?? ??? ?return match(regex, str);
?? ?}
?? ?
?? ?
?? ?/**
?? ? * 對開戶行名稱如:102中國工商銀行進(jìn)行校驗(只含有數(shù)字和漢字校驗)
?? ? * @param str
?? ? * @return
?? ? */
?? ?public static boolean isChineseAndIsNum(String str) {
?? ??? ? String regEx = "^[0-9\u4e00-\u9fa5]+$";
?? ??? ?Pattern pat = Pattern.compile(regEx);
?? ??? ?Matcher matcher = pat.matcher(str);
?? ??? ?if(matcher.find()) {
?? ??? ??? ?return true;
?? ??? ?}else {
?? ??? ??? ?return false;
?? ??? ?}
?? ?}
?? ?
?? ?/**
?? ? * 2018-08-08
?? ? * 對日期格式進(jìn)行判斷
?? ? * @param date
?? ? * @return
?? ? */
?? ?public boolean isDate(String date)
? ? {
? ? ? /**
? ? ? ?* 判斷日期格式和范圍
? ? ? ?*/
? ? ? String rexp = "^((\\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 pat = Pattern.compile(rexp);
? ? ? Matcher mat = pat.matcher(date);
? ? ? boolean dateType = mat.matches();
? ? ? return dateType;
? ? }
?? ?
?? ?/**
?? ? * 對銀行賬號信息校驗
?? ? * @param bankName
?? ? * @return
?? ? */
?? ?public static boolean idBankId(String bankAccount,String bankName ) {
?? ??? ?int a=6;
?? ??? ?if(bankAccount.length()<a) {
?? ??? ??? ?return false;
?? ??? ?}
?? ??? ?String str=bankAccount.substring(0, 6);
?? ??? ?String str1="402農(nóng)村信用社";
?? ??? ?String str2="623017";
?? ??? ?String str3="623213";
?? ??? ?int str4=16;
?? ??? ?if((str1).equals(bankName)) {?? ??? ??? ??? ?
?? ??? ??? ?if((str2).equals(str)||(str3).equals(str)) {
?? ??? ??? ??? ?if(bankAccount.length()!=str4) {
?? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}?? ??? ??? ??? ?
?? ??? ??? ?}else {
?? ??? ??? ??? ?return false; ? ? ??
?? ??? ??? ?}
?? ??? ?}
?? ??? ?String jsyh="105中國建設(shè)銀行";
?? ??? ?String str5="631700";
?? ??? ?String str6="622280";
?? ??? ?String str7="436742";
?? ??? ?String js1="621467";
?? ??? ?String js2="621700";
?? ??? ?String js3="621081";
?? ??? ?String js4="622700";
?? ??? ?String js5="623668";
?? ??? ?String js6="442228";
?? ??? ?int str8=19;
?? ??? ?String js7="622707";
?? ??? ?String js8="434062";
?? ??? ?if((jsyh).equals(bankName)) {
?? ??? ??? ?if((str5).equals(str)||(str6).equals(str)||(str7).equals(str)||(js1).equals(str)
?? ??? ?||(js2).equals(str)||(js3).equals(str)||(js4).equals(str)||(js5).equals(str)||(js6).equals(str)) {
?? ??? ??? ??? ?if(bankAccount.length()!=str8) {
?? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}
?? ??? ??? ?}else if((js7).equals(str)||(js8).equals(str)){
?? ??? ??? ??? ?if(bankAccount.length()!=str4) {
?? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}
?? ??? ??? ?}else {
?? ??? ??? ??? ?return false;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?String nyyh="103中國農(nóng)業(yè)銀行";
?? ??? ?String s="622848";
?? ??? ?String s1="622841";
?? ??? ?String s8="623052";
?? ??? ?String s9="622845";
?? ??? ?String s10="622846";
?? ??? ?String s11="622827";
?? ??? ?String s12="621671";
?? ??? ?String s13="955998";
?? ??? ?String s14="281010";
?? ??? ?int s15=17;
?? ??? ?if((nyyh).equals(bankName)) {
?? ??? ??? ?if((s).equals(str)||(s1).equals(str)||(s8).equals(str)||(s9).equals(str)
?? ??? ??? ??? ??? ?||(s10).equals(str)||(s11).equals(str)||(s12).equals(str)||(s13).equals(str)) {
?? ??? ??? ??? ?if(bankAccount.length()!=str8) {
?? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}
?? ??? ??? ?}else if((s14).equals(str) && bankAccount.length()==s15){
?? ??? ??? ??? ?return true;
?? ??? ??? ?}else {
?? ??? ??? ??? ?return false;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?String yzyh="403中國郵政儲蓄銀行";
?? ??? ?String s2="621799";
?? ??? ?String s3="608561";
?? ??? ?String s4="622150";
?? ??? ?if((yzyh).equals(bankName)) {
?? ??? ??? ?if((s2).equals(str)||(s3).equals(str)||(s4).equals(str)) {
?? ??? ??? ??? ?if(bankAccount.length()!=str8) {
?? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}
?? ??? ??? ?}else {
?? ??? ??? ??? ?return false;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?String zgyh="104中國銀行";
?? ??? ?String zh1="621785";
?? ??? ?String zh2="621226";
?? ??? ?String zh3="621725";
?? ??? ?String zh4="623569";
?? ??? ?String zh5="621786";
?? ??? ?String zh6="621661";
?? ??? ?String zh7="621660";
?? ??? ?if((zgyh).equals(bankName)) {
?? ??? ??? ?if((zh1).equals(str)||(zh2).equals(str)||(zh3).equals(str)||(zh4).equals(str)
?? ??? ??? ??? ??? ?||(zh5).equals(str)||(zh6).equals(str)||(zh7).equals(str)) {
?? ??? ??? ??? ?if(bankAccount.length()!=str8) {
?? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}
?? ??? ??? ?}else {
?? ??? ??? ??? ?return false;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?String gsyh="102中國工商銀行";
?? ??? ?String gs1="621723";
?? ??? ?String gs2="621226";
?? ??? ?String gs3="622203";
?? ??? ?String gs4="622208";
?? ??? ?String gs5="280600";
?? ??? ?String gs6="622597";
?? ??? ?String gs7="621722";
?? ??? ?if((gsyh).equals(bankName)) {
?? ??? ??? ?if((gs1).equals(str)||(gs2).equals(str)||(gs3).equals(str)||(gs4).equals(str)
?? ??? ??? ??? ??? ?||(gs5).equals(str)||(gs7).equals(str)) {
?? ??? ??? ??? ?if(bankAccount.length()!=str8) {
?? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ?return true;
?? ??? ??? ??? ?}
?? ??? ??? ?}else if((gs6).equals(str) && bankAccount.length()==str4) {
?? ??? ??? ??? ?return true;
?? ??? ??? ?}else {
?? ??? ??? ??? ?return false;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?String jtyh="301交通銀行";
?? ??? ?String jt="622262";
?? ??? ?if((jtyh).equals(bankName)) {
?? ??? ??? ?if((jt).equals(str) && bankAccount.length()==str8) {
?? ??? ??? ??? ?return true;
?? ??? ??? ?}else {
?? ??? ??? ??? ?return false;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?//其他銀行不作校驗判斷
?? ??? ?return true;?? ??? ??? ?
?? ?}
?? ?
?? ?/**
?? ?* @param regex
?? ?* 正則表達(dá)式字符串
?? ?* @param str
?? ?* 要匹配的字符串
?? ?* @return 如果str 符合 regex的正則表達(dá)式格式,返回true, 否則返回 false;
?? ?*/
?? ?private static boolean match(String regex, String str) {
?? ??? ?Pattern pattern = Pattern.compile(regex);
?? ??? ?Matcher matcher = pattern.matcher(str);
?? ??? ?return matcher.matches();
?? ?}
}
?
?
總結(jié)
以上是生活随笔為你收集整理的java 代码实现各数据的正则校验的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 生命存在的意义
- 下一篇: 嵌入式开发中的防御性C语言编程