Comparta una clase de herramientas que use expresiones regulares para verificar el número de teléfono, el número de identificación, el formato de fecha, la URL, el correo electrónico y otros formatos
La copia del código es la siguiente:
paquete com.eabax.util;
import java.util.regex.matcher;
import java.util.regex.pattern;
/**
* Clase de herramienta de verificación
* @Author Admin
*
*/
Validación de clase pública {
// ------------------------------------------------ -------------------------------------------------- ---------------------------
/**
* Correo electrónico expresión regular = "^([a-z0-9a-z]+[-| //.]?]?)+[A-z0-9a-z]@([a-z0-9a-z]+( -[A-Z0-9A-Z]+)? //.)+[A-ZA-Z] {2,} $ ";
*/
// public static final String Correo electrónico = "^([a-z0-9a-z]+[-| // ]+(-[A-Z0-9A-Z]+)? //.)+[A-ZA-Z] {2,} $ ";;
public static final String Correo electrónico = "//w+(//.//w+)*@/w+(//.//w+)+";
/**
* Número de teléfono Regular Expression = (^(/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} $) ";
/**
* Número de teléfono móvil expresión regular =^(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} $";
/**
* Expresión regular de entero ^-? (([1-9]/d* $) | 0)
*/
public static final String entero = "^-? (([1-9] // d*$) | 0)";
/**
* Expresión regular de entero positivo> = 0 ^[1-9]/d* | 0 $
*/
public static final String integer_negative = "^[1-9] // d*| 0 $";
/**
* Expresión regular del entero negativo <= 0 ^-[1-9]/d* | 0 $
*/
public static final String integer_positivo = "^-[1-9] // d*| 0 $";
/**
*Expresión regular doble ^-? ([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) $ ";
/**
*Expresión regular doble> = 0 ^[1-9]/d*/./D*| 0 /./ d*[1-9]/d*| 0?/. 0+ | 0 $
*/
cadena final estática pública double_negative = "^[1-9] // d*//. // d*| 0 //.// d*[1-9] // d*| 0? //. 0+ | 0 $ ";
/**
*Expresión regular doble negativa <= 0 ^(-([1-9]/d*/./D*| 0 /./ d*[1-9]/d*)) | 0?/. 0+ | 0 $
*/
Cadena final Public Static Final Double_positive = "^(-([1-9] // d*//. // d*| 0 //.// d*[1-9] // d*)) | 0? //.0+|0$ ";
/**
* Expresión regular de edad^(?: [1-9] [0-9]? | 1 [01] [0-9] | 120) $ Match 0-120 años
*/
public static final String Age = "^(?: [1-9] [0-9]? | 1 [01] [0-9] | 120) $";
/**
* Código postal de expresión regular [0-9]/D {5} (?/D) Código postal doméstico de 6 dígitos
*/
public static final String Code = "[0-9] // d {5} (?! // d)";
/**
* Haga coincidir una cadena compuesta de números, 26 letras en inglés o subrayadores ^/W+$ $
*/
Cadena final estática pública str_eng_num _ = "^// w+$";
/**
* Haga coincidir una cadena compuesta de números y 26 letras inglesas^[A-ZA-Z0-9]+$
*/
Public static final String str_eng_num = "^[A-ZA-Z0-9]+";
/**
* Haga coincidir la cadena que consta de 26 letras inglesas^[A-Za-Z]+$
*/
Public static final String str_eng = "^[A-Za-Z]+$";
/**
* Filtrar regularidad de cadena especial
*regex = "[` ~!@#$%^&*()+= | {} ':;', // [//]. <>/? ~!##¥%... &*() - - - - - - - - +| {} 【】 ';: "'. ,?]";
*/
public static final String str_special = "[` ~!#$%^& *()+= | {} ':;', // [//]. <>/? ~!#¥%... & * () -+| {} 【】 ';: "".
/***
* Fecha de apoyo regular:
* Aaa yyy-mm-dd
* Aaa yyy/mm/dd
* Aaa yyy_mm_dd
* Yyyymmdd
* Forma de yyyy.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) $)) ";
/***
* Fecha de apoyo regular:
* Aaa yyy-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) ";
/**
* URL Expresión regular
* Coincidir con http www ftp
*/
public static final String url = "^(http | www | ftp |)? (: //)? (// w+(-// w+)*) (//. (// w+(-// w+)* ))*((: // d+)?) (/(// w+(-// w+)*))*(//.? (// w)*) (//?) (//?) ? " +
"((((//w*%)*(//w*//?)*(//w*:)*(//w*//+)*(//w*//.)* (// w*&)*(// w*-)*(// w*=)*(// w*%)*(// w*//?)*" +
"(//w*:)*(//w*//+)*(//w*//.)*" +
"(// w*&)*(// w*-)*(// w*=)*)*(// w*)*) $";
/**
* ID de expresión regular de la tarjeta de identificación
*/
Cadena final pública estática 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}))";
/**
* Código institucional
*/
public static final String jigou_code = "^[a-z0-9] {8}-[a-z0-9] $";
/**
* Haga coincidir la cadena ^[0-9]+$
*/
public static final String str_num = "^[0-9]+$";
//// ------------------------------------------ -------------------------------------------------- ---------------------------
/**
* Determinar si el campo está vacío y coincide con el return ture
* @param str
* @return boolean
*/
Public static sincronizado boolean strisnull (string str) {
return null == str || str.trim (). longitud () <= 0?
}
/**
* Devuelve el ture si el campo no está vacío
* @param str
* @return boolean
*/
Public static boolean strnotnull (string str) {
Return! Strisnull (str);
}
/**
* Cadena nula a vacío
* @param str
* @return boolean
*/
public static string nullToStr (String str) {
return strisnull (str)? "": str;
}
/**
* Valor predeterminado de la asignación nula de cadena
* @Param STR String Target String
* @param defaut valor predeterminado
* @return cadena
*/
public static string nullToStr (string str, string defaut) {
return strisnull (str)? defaut: str;
}
/**
* Determinar si el campo es correo electrónico y devuelve el ture
* @param str
* @return boolean
*/
Public static boolean isEmail (string str) {
regresar regular (str, correo electrónico);
}
/**
* Determinar si el número de teléfono cumple con el retorno de
* @param str
* @return boolean
*/
Public Boolean isphone (String Str) {
regresar regular (str, teléfono);
}
/**
* Determinar si el número de teléfono móvil cumple con el estado
* @param str
* @return boolean
*/
Public static boolean isMobile (string str) {
regresar regular (str, móvil);
}
/**
* Determinar si es URL y devuelve la TURE
* @param str
* @return boolean
*/
Public static boolean isurl (string str) {
regresar regular (str, url);
}
/**
* Determine si el campo es un número de entero positivo y negativo, un número de punto flotante positivo y negativo, devuelve el ture
* @param str
* @return boolean
*/
Public static boolean isnumber (string str) {
regresar regular (str, doble);
}
/**
* Determinar si el campo es entero y devuelve el ture
* @param str
* @return boolean
*/
Public static boolean isInteger (string str) {
regresar regular (str, entero);
}
/**
* Determine si el campo es una expresión regular de entero positivo> = 0 y coincide con la ture de retorno
* @param str
* @return boolean
*/
public static boolean isInteger_Negative (String str) {
return regular (str, integer_negative);
}
/**
* Determine si el campo es una expresión regular de entero negativo <= 0 y coincide con la tura de retorno
* @param str
* @return boolean
*/
public static boolean isInteger_positivo (String Str) {
return regular (str, integer_positivo);
}
/**
* Determinar si el campo es doble y devuelve el ture
* @param str
* @return boolean
*/
Public static boolean isDouble (string str) {
regresar regular (str, doble);
}
/**
* Determine si el campo es un número de punto flotante positivo expresión regular> = 0 para que coincida con la ture de retorno
* @param str
* @return boolean
*/
Public static boolean isDouble_Negative (String str) {
return regular (str, double_negative);
}
/**
* Determinar si el campo es un número de punto flotante negativo de la expresión regular <= 0 y coincide con el retorno ture
* @param str
* @return boolean
*/
public static boolean isDouble_positivo (String Str) {
devolver regular (str, double_positivo);
}
/**
* Determinar si el campo está fechado y devuelve el ture
* @param str
* @return boolean
*/
Public static boolean isDate (string str) {
regresar regular (str, date_all);
}
/**
* Verificación 2010-12-10
* @param str
* @devolver
*/
Public static boolean isDate1 (string str) {
return regular (str, date_format1);
}
/**
* Vuelva al ture si el campo es compatible con la edad
* @param str
* @return boolean
*/
Public static boolean isage (string str) {
regresar regular (str, edad);
}
/**
* Determinar si el campo es demasiado largo
* Si la cadena está vacía, devuelve fasle, si excede la longitud {Leng}, devuelve ture, y si devuelve falso
* @param str
* @param leng
* @return boolean
*/
Public static boolean islengout (string str, int longitud) {
return strisnull (str)? falso: str.trim (). longitud ()> longitud;
}
/**
* Determinar si el campo es una tarjeta de identificación y devuelve el ture
* @param str
* @return boolean
*/
Public static boolean isidcard (string str) {
if (strisnull (str)) return false;
if (str.trim (). longitud () == 15 || str.trim (). longitud () == 18) {
regresar regular (str, idcard);
}demás {
devolver falso;
}
}
/**
* Determinar si el campo tiene un código postal que coincide con la tre de retorno
* @param str
* @return boolean
*/
Public static boolean iscode (string str) {
regresar regular (str, código);
}
/**
* Determinar si todas las cadenas son letras en inglés
* @param str
* @return boolean
*/
Public static boolean isenglish (string str) {
regresar regular (str, str_eng);
}
/**
* Determinar si todas las cadenas son letras en inglés + números
* @param str
* @return boolean
*/
Public static boolean iseng_num (string str) {
return regular (str, str_eng_num);
}
/**
* Determinar si todas las cadenas son letras en inglés + números + subrayadores
* @param str
* @return boolean
*/
public static boolean iseng_num_ (string str) {
return regular (str, str_eng_num_);
}
/**
* Filtar cadenas especiales de retorno cadenas filtradas
* @param str
* @return boolean
*/
public static string filterStr (string str) {
Patrón p = patrón.compile (str_special);
Matcher M = p.matcher (STR);
return M.Replaceall (""). Trim ();
}
/**
* Verifique el formato de código del mecanismo
* @devolver
*/
Public static boolean isJigoUcode (string str) {
return regular (str, jigou_code);
}
/**
* Determinar si la cadena está compuesta de números
* @param str
* @return boolean
*/
Public static boolean isstr_num (string str) {
regresar regular (str, str_num);
}
/**
* Si la coincidencia se ajusta al patrón de expresión regular.
* @Param STR Cadena coincidente
* @Param Pattern Patrón de coincidencia
* @return boolean
*/
Regular de booleano estático privado (String Str, String Pattern) {
if (null == str || str.trim (). longitud () <= 0)
devolver falso;
Patrón p = patrón.compile (patrón);
Matcher M = p.matcher (STR);
return m.matches ();
}
}
Lo anterior se trata de este artículo, espero que les guste.