Partagez une classe d'outils qui utilise des expressions régulières pour vérifier le numéro de téléphone, le numéro d'identification, le format de date, l'URL, les e-mails et autres formats
La copie de code est la suivante:
package com.eabax.util;
import java.util.regex.matcher;
import java.util.regex.pattern;
/ **
* Classe d'outils de vérification
* @author admin
*
* /
Validation de classe publique {
// ------------------------------------------------ -------------------------------------------------- ---------------------------
/ **
* Email régulier expression = "^ ([A-Z0-9A-Z] + [- | //.]?) + [A-Z0-9A-Z] @ ([A-Z0-9A-Z] + ( - [a-z0-9a-z] +)? //.) + [a-za-z] {2,} $ ";
* /
// public static final String e-mail = "^ ([A-Z0-9A-Z] + [- | //.]?) + [A-Z0-9A-Z] @ ([A-Z0-9A-Z ] + (- [a-z0-9a-z] +)? //.) + [a-za-z] {2,} $ ";;
public static final String email = "//w+(//./w+)*//w+(////w+)+";
/ **
* Numéro de téléphone Expression régulière = (^ (/ d {2,4} [-_----]?)? / D {3,8} ([-_-----]? / D {3, 8})?
* /
Public Static Final String Phone = "(^ (// d {2,4} [-_----]?)? // d {3,8} ([-_-----]? // d {3,8})? ([-_---]? // d {1,7})? $) | (^ 0? 1 [35] // d {9} $) ";
/ **
* Numéro de téléphone mobile Expression régulière = ^ (13 [0-9] | 15 [0-9] | 18 [0-9]) / D {8} $
* /
public static final String mobile = "^ (13 [0-9] | 15 [0-9] | 18 [0-9]) // d {8} $";
/ **
* Expression régulière entière ^ -? (([1-9] / d * $) | 0)
* /
public static final String Integer = "^ -? (([1-9] // d * $) | 0)";
/ **
* Expression régulière entière positive> = 0 ^ [1-9] / d * | 0 $
* /
public static final String Integer_negative = "^ [1-9] // d * | 0 $";
/ **
* Expression régulière entière négative <= 0 ^ - [1-9] / d * | 0 $
* /
public static final String Integer_Positive = "^ - [1-9] // d * | 0 $";
/ **
* Double expression régulière ^ -? ([1-9] / d * /. / D * | 0 /./ d * [1-9] / d * | 0? /. 0+ | 0) $
* /
Public Static Final String double = "^ -? ([1-9] // d * //. // d * | 0 //.// d * [1-9] // d * | 0? // .0+ | 0) $ ";
/ **
* Double Expression régulière> = 0 ^ [1-9] / d * /. / D * | 0 /./ d * [1-9] / d * | 0? /. 0+ | 0 $
* /
public static final String double_negative = "^ [1-9] // d * //. // d * | 0 //.// d * [1-9] // d * | 0? //. 0+ | 0 $ ";
/ **
* Expression négative à double régulière <= 0 ^ (- ([1-9] / d * /. / D * | 0 /./ d * [1-9] / d *)) | 0? /. 0+ | 0 $
* /
Public Static Final String double_positive = "^ (- ([1-9] // d * //. // d * | 0 //.// d * [1-9] // d *)) | 0? //.0+|0$ ";
/ **
* Expression régulière de l'âge ^ (?: [1-9] [0-9]? | 1 [01] [0-9] | 120) $ Match 0-120 ans
* /
public static final String Age = "^ (?: [1-9] [0-9]? | 1 [01] [0-9] | 120) $";
/ **
* Code postal Expression régulière [0-9] / D {5} (?! / D) Code postal domestique à 6 chiffres
* /
Code de chaîne finale statique publique = "[0-9] // d {5} (?! // d)";
/ **
* Faites correspondre une chaîne composée de nombres, 26 lettres ou soulignements anglais ^ / w + $
* /
chaîne finale statique publique str_eng_num _ = "^ // w + $";
/ **
* Faites correspondre une chaîne composée de nombres et 26 lettres anglaises ^ [a-za-z0-9] + $
* /
chaîne finale statique publique str_eng_num = "^ [a-za-z0-9] +";
/ **
* Faites correspondre la chaîne composée de 26 lettres anglaises ^ [a-za-z] + $
* /
chaîne finale statique publique str_eng = "^ [a-za-z] + $";
/ **
* Filtre la régularité de la chaîne spéciale
* regex = "[` ~! @ # $% ^ & * () + = | {} ':;', // [//]. <> /? ~! @ # ¥%… & * () ― + | {} 【】 ';: "'. ,?]";
* /
Public Static Final String str_special = "[` ~! @ # $% ^ & * () + = | {} ':;', // [//]. <> /? ~! @ # ¥%… & * () - + | {} 【】 '; "" ".
/ ***
* Date Support régulier:
* Yyyy-mm-dd
* Yyyy / mm / dd
* Yyyy_mm_dd
* Yyyymmdd
* Forme de Yyy.mm.dd
* /
public static final String date_all = "(^ ((1 [8-9] // d {2}) | ([2-9] // d {3})) ([- //// ._ ]?) (10 | 12 | 0? [13578]) ([- ////// ._]?) (3 [01] | [12] [0-9] | 0? [1-9] ) $) "+
"| (^ ((1 [8-9] // d {2}) | ([2-9] // d {3})) ([- //// ._]?) (11 | 0? [469]) ([- ///// ._]?) (30 | [12] [0-9] | 0? [1-9]) $) "+
"| (^ ((1 [8-9] // d {2}) | ([2-9] // d {3})) ([- //// ._]?) (0? 2) ([- ///// ._]?) (2 [0-8] | 1 [0-9] | 0? [1-9]) $) | (^ ([2468] [048] 00) ([- ////// ._]?) (0? 2) ([- ///// .__]?) (29) $) | (^ ([3579] [26] 00) "+
"([- ////// ._]?) (0? 2) ([- ////// ._]?)?) (29) $)" +
"| (^ ([1] [89] [0] [48]) ([- ////// ._]?) (0? 2) ([- ////// ._]? ) (29) $) | (^ ([2-9] [0-9] [0] [48]) ([- ///// ._]?) "+
"(0? 2) ([- ///// ._]?) (29) $)" +
"| (^ ([1] [89] [2468] [048]) ([- ///// .__]?) (0? 2) ([- ////// .__]? ) (29) $) | (^ ([2-9] [0-9] [2468] [048]) ([- //// ._]?) (0? 2) "+
"([- ///// ._]?) (29) $) | (^ ([1] [89] [13579] [26]) ([- ////// .__]?)? (0? 2) ([- ///// ._]?) (29) $) | "+
"(^ ([2-9] [0-9] [13579] [26]) ([- //// ._)?) (0? 2) ([- ////// .__ ]?) (29) $)) ";
/ ***
* Date Support régulier:
* Yyyy-mm-dd
* /
Public Static Final String date_format1 = "(([0-9] {3} [1-9] | [0-9] {2} [1-9] [0-9] {1} | [0-9] {1} [1-9] [0-9] {2} | [1-9] [0-9] {3}) - ((0 [13578] | 1 [02]) - (0 [1 - 9] | [12] [0-9] | 3 [01])) | ((0 [469] | 11) - (0 [1-9] | [12] [0-9] | 30)) | (02- (0 [1-9] | [1] [0-9] | 2 [0-8])))) | ((([0-9] {2}) (0 [48] | [ 2468] [048] | [13579] [26]) | ((0 [48] | [2468] [048] | [3579] [26]) 00)) - 02-29) ";
/ **
* Expression régulière d'URL
* Match http www ftp
* /
Url de chaîne finale statique publique = "^ (http | www | ftp |)? (: //)? (// w + (- // w +) *) (//. (// w + (- // w +) * ))) * ((: // d +)?) (/ (// w + (- // w +) *)) * (//.? (// w) *) (//?) (//?) ? "+
"((((//w*%)* (//w*//?)* (//w*:)* loin (// w * &) * (// w * -) * (// w * =) * (// w *%) * (// w * //?) * "+
"(//w* :)* (//w*//+)* (//w*//.)*" +
"(// w * &) * (// w * -) * (// w * =) *) * (// w *) *) $";
/ **
* Expression régulière de la carte d'identité
* /
Public Static Final String idcard = "((11 | 12 | 13 | 14 | 15 | 21 | 22 | 23 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 41 | 42 | 43 | 44 | 45 | 46 | 50 | 51 | 52 | 53 | 54 | 61 | 62 | 63 | 64 | 65) [0-9] {4}) "+
"(([1 | 2] [0-9] {3} [0 | 1] [0-9] [0-3] [0-9] [0-9] {3}" +
"[Xx0-9]) | ([0-9] {2} [0 | 1] [0-9] [0-3] [0-9] [0-9] {3}))";
/ **
* Code institutionnel
* /
String final statique publique jigou_code = "^ [a-z0-9] {8} - [a-z0-9] $";
/ **
* Faites correspondre la chaîne ^ [0-9] + $
* /
chaîne finale statique publique str_num = "^ [0-9] + $";
//// ---------------------------------------------- -------------------------------------------------- ---------------------------
/ **
* Déterminez si le champ est vide et correspond au retour
* @param str
* @return boolean
* /
STRISNULL BOOLEAN SYNCHRONISÉ STATIQUE PUBLIQUE (String Str) {
retourner null == Str || Str.trim (). Longueur () <= 0?
}
/ **
* Renvoyez la ture si le champ n'est pas vide
* @param str
* @return boolean
* /
public statique booléen strnotnull (String str) {
retour! Strisnull (str);
}
/ **
* String null to vide
* @param str
* @return boolean
* /
String statique publique nullToStr (String str) {
Retour Strisnull (Str)? "": Str;
}
/ **
* String Null Affectation Valeur par défaut
* @param str de chaîne cible
* @Param Defaut Valeur par défaut
* String @return
* /
String statique publique nullToStr (String Str, String Defaut) {
retour strisnull (str)? Defaut: str;
}
/ **
* Déterminez si le champ est par e-mail et renvoie la Ture
* @param str
* @return boolean
* /
booléen statique publique iSemail (String str) {
Retour régulier (str, e-mail);
}
/ **
* Déterminez si le numéro de téléphone répond au retour
* @param str
* @return boolean
* /
Public Static Boolean Isphone (String Str) {
Retour régulier (str, téléphone);
}
/ **
* Déterminez si le numéro de téléphone mobile répond à l'état
* @param str
* @return boolean
* /
booléen statique publique Ismobile (String Str) {
Retour régulier (str, mobile);
}
/ **
* Déterminez s'il s'agit de l'URL et renvoie la ture
* @param str
* @return boolean
* /
booléen statique public Isurl (String str) {
retour régulièrement (str, url);
}
/ **
* Déterminez si le champ est un nombre, un entier positif et négatif, un nombre de points flottants positifs et négatifs, renvoyez la ture
* @param str
* @return boolean
* /
booléen statique publique ISNumber (String str) {
retour régulier (str, double);
}
/ **
* Déterminez si le champ est entier et renvoie la ture
* @param str
* @return boolean
* /
booléen statique public est IsInteger (String Str) {
Retour régulier (str, entier);
}
/ **
* Déterminez si le champ est une expression régulière entière positive> = 0 et correspond au retour
* @param str
* @return boolean
* /
public static boolean isInteger_negative (String Str) {
retour régulièrement (str, Integer_negative);
}
/ **
* Déterminez si le champ est une expression régulière en entier négative <= 0 et correspond à la retour
* @param str
* @return boolean
* /
public static boolean isInteger_Positive (String str) {
retour régulièrement (str, Integer_Positive);
}
/ **
* Déterminez si le champ est double et renvoie la ture
* @param str
* @return boolean
* /
Isdouble booléen statique public (String Str) {
retour régulier (str, double);
}
/ **
* Déterminez si le champ est un nombre de points flottants positifs Expression régulière> = 0 pour correspondre au retour
* @param str
* @return boolean
* /
public statique booléen isdouble_negative (String str) {
retour régulièrement (str, double_negative);
}
/ **
* Déterminez si le champ est un nombre de points flottants négatifs d'expression régulière <= 0 et correspond au retour
* @param str
* @return boolean
* /
public statique booléen isdouble_positive (String str) {
retour régulièrement (str, double_positif);
}
/ **
* Déterminez si le champ est daté et renvoie la ture
* @param str
* @return boolean
* /
Isdate booléen statique public (String Str) {
retour régulièrement (str, date_all);
}
/ **
* Vérification 2010-12-10
* @param str
* @retour
* /
public statique booléen isdate1 (String str) {
return régulier (str, date_format1);
}
/ **
* Retour à la ture si le champ est conforme à l'âge
* @param str
* @return boolean
* /
ISAGE BOOLEAN STATIQUE STATIQUE (String Str) {
Retour régulier (Str, Age);
}
/ **
* Déterminez si le champ est trop long
* Si la chaîne est vide, elle renvoie Fasle, si elle dépasse la longueur {Leng}, elle revient, et si elle revient fausse
* @param str
* @param Leng
* @return boolean
* /
booléen statique publique Islengout (String Str, int longueur) {
return strisnull (str)? false: str.trim (). length ()> longueur;
}
/ **
* Déterminez si le champ est une carte d'identité et renvoie la Ture
* @param str
* @return boolean
* /
public statique booléen isidcard (String str) {
if (strisnull (str)) renvoie false;
if (str.trim (). Length () == 15 || str.trim (). Length () == 18) {
Retour régulier (str, idcard);
}autre {
retourne false;
}
}
/ **
* Déterminez si le champ a un code postal qui correspond au retour
* @param str
* @return boolean
* /
booléen statique public iscode (String str) {
retour régulièrement (str, code);
}
/ **
* Déterminez si toutes les chaînes sont des lettres anglaises
* @param str
* @return boolean
* /
public statique booléen isenglish (String str) {
retour régulièrement (str, str_eng);
}
/ **
* Déterminez si toutes les chaînes sont des lettres anglaises + numéros
* @param str
* @return boolean
* /
public static boolean iseng_num (String str) {
retour régulier (str, str_eng_num);
}
/ **
* Déterminez si toutes les chaînes sont des lettres anglaises + numéros + soulignements
* @param str
* @return boolean
* /
public static boolean iseng_num_ (String str) {
retour régulièrement (str, str_eng_num_);
}
/ **
* Filtrer les chaînes spéciales Retour des chaînes filtrées
* @param str
* @return boolean
* /
Public Static String Filterstr (String Str) {
Pattern p = motive.compile (str_special);
Matcher m = p.matcher (str);
retour m.replaceALL (""). Trim ();
}
/ **
* Vérifiez le format de code du mécanisme
* @retour
* /
booléen statique publique Isjigoucode (String str) {
retour régulièrement (str, jigou_code);
}
/ **
* Déterminez si la chaîne est composée de nombres
* @param str
* @return boolean
* /
public statique booléen isstr_num (String str) {
retour régulier (str, str_num);
}
/ **
* Si le match est conforme au modèle d'expression régulière.
* @Param Str Matching String
* motif de correspondance du modèle @param
* @return boolean
* /
Boolean statique privé régulier (String Str, modèle de chaîne) {
if (null == Str || str.trim (). Length () <= 0)
retourne false;
Motif p = motif.compile (motif);
Matcher m = p.matcher (str);
retour m.matches ();
}
}
Ce qui précède est tout sur cet article, j'espère que vous l'aimez.