日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java 代码实现各数据的正则校验

發(fā)布時(shí)間:2024/3/7 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 代码实现各数据的正则校验 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最近公司的項(xiàng)目,關(guān)于excel導(dǎo)入導(dǎo)出,對(duì)數(shù)據(jù)要進(jìn)行校驗(yàn),所以就對(duì)數(shù)據(jù)的正則校驗(yàn)就用的比較多,自己也是一點(diǎn)點(diǎn)去查,現(xiàn)在項(xiàng)目完成了,就把所有用到的都做一個(gè)小結(jié),或許以后還可以用到!

package org.asyware.insurance.util;

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

/**
?*?
?* @author zl
?*?
?*
?*/
public class DataCheckUtil {

?/**
? ? ?* @see 驗(yàn)證手機(jī)號(hào)是否合法
? ? ?* @param mobiles
? ? ?* @return ?合法:true ?不合法:false
? ? ?*/
? ? public static boolean isMobileNO(String mobile){ ?
? ? ? ? if (mobile.length() != 11)
? ? ? ? {
? ? ? ? ? ? return false;
? ? ? ? }else{
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 移動(dòng)號(hào)段正則表達(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)通號(hào)段正則表達(dá)式
? ? ? ? ? ? ?*/
? ? ? ? ? ? String pat2 ?= "^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 電信號(hào)段正則表達(dá)式
? ? ? ? ? ? ?*/
? ? ? ? ? ? String pat3 ?= "^((133)|(153)|(177)|(18[0,1,9])|(149)|(199))\\d{8}$";
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 虛擬運(yùn)營(yíng)商正則表達(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;
?? ??? ?}??
?? ?}
?? ?


?? ?/**
?? ? * 卡號(hào)校驗(yàn)是否是純數(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);
?? ?}
?? ?
?? ?
?? ?/**
?? ? * 對(duì)開戶行名稱如:102中國(guó)工商銀行進(jìn)行校驗(yàn)(只含有數(shù)字和漢字校驗(yàn))
?? ? * @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
?? ? * 對(duì)日期格式進(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;
? ? }
?? ?
?? ?/**
?? ? * 對(duì)銀行賬號(hào)信息校驗(yàn)
?? ? * @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中國(guó)建設(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中國(guó)農(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中國(guó)郵政儲(chǔ)蓄銀行";
?? ??? ?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中國(guó)銀行";
?? ??? ?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中國(guó)工商銀行";
?? ??? ?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;
?? ??? ??? ?}
?? ??? ?}
?? ??? ?//其他銀行不作校驗(yàn)判斷
?? ??? ?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 代码实现各数据的正则校验的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。