Compartilhe uma classe de ferramentas que usa expressões regulares para verificar o número de telefone, número de identificação, formato de data, URL, email e outros formatos
A cópia do código é a seguinte:
pacote com.eabax.util;
importar java.util.regex.matcher;
importar java.util.regex.pattern;
/**
* Classe da ferramenta de verificação
* @Author Admin
*
*/
classe pública Validação {
// -------------------------------------------------- -------------------------------------------------------- -----------------------------
/**
* Email Expression 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 email = "^([a-z0-9a-z]+[-| //.]?)+[a-z0-9a-z]@([a-z0-9a-zz ]+(-[a-z0-9a-z]+)? //.)+[a-za-z] {2,} $ ";;
public static final string email = "//w+(//.//w+)*///w+(//.//w+)+";
/**
* Número de telefone Expressão regular = (^(/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 do celular Expressão regular =^(13 [0-9] | 15 [0-9] | 18 [0-9])/D {8} $
*/
public static final string móvel = "^(13 [0-9] | 15 [0-9] | 18 [0-9]) // d {8} $";
/**
* Expressão regular inteira ^-? (([1-9]/d* $) | 0)
*/
Public Static Final String Integer = "^-? (([1-9] // D*$) | 0)";
/**
* Expressão regular do número inteiro positivo> = 0 ^[1-9]/d* | 0 $
*/
public static final string intege_negative = "^[1-9] // d*| 0 $";
/**
* Expressão regular do número inteiro negativo <= 0 ^-[1-9]/d* | 0 $
*/
public static final string INTEGER_POSITIVO = "^-[1-9] // D*| 0 $";
/**
*Expressão regular dupla ^-? ([1-9]/d*/./D*| 0 /./ d*[1-9]/d*| 0?/. 0+ | 0) $
*/
public static final string duplo = "^-? ([1-9] // d*//. // d*| 0 //.// d*[1-9] // d*| 0? // .0+ | 0) $ ";
/**
*Expressão regular dupla> = 0 ^[1-9]/d*/./D*| 0 /./ d*[1-9]/d*| 0?/. 0+ | 0 $
*/
public static final string duplo_negative = "^[1-9] // d*//. // d*| 0 //.// d*[1-9] // d*| 0? //. 0+ | 0 $ ";
/**
*Expressão regular dupla negativa <= 0 ^(-([1-9]/d*/./D*| 0 /./ d*[1-9]/d*)) | 0?/. 0+ | 0 $
*/
public static final string duplo_positivo = "^(-([1-9] // d*//. // d*| 0 //.// d*[1-9] // d*)) | 0? //.0+|0$ ";
/**
* Expressão regular da idade^(?: [1-9] [0-9]? | 1 [01] [0-9] | 120) $ Match 0-120 anos de idade
*/
public static final string Age = "^(?: [1-9] [0-9]? | 1 [01] [0-9] | 120) $";
/**
* Código postal Expressão regular [0-9]/D {5} (?!/D) Código postal de 6 dígitos domésticos
*/
public static final string code = "[0-9] // d {5} (?! // d)";
/**
* Combine uma string composta de números, 26 letras em inglês ou sublinhado ^/w+$ $
*/
public static final string str_eng_num _ = "^// w+$";
/**
* Combine uma string composta por números e 26 letras em inglês^[a-ZA-Z0-9]+$
*/
public static final string str_eng_num = "^[a-za-z0-9]+";
/**
* Combine a string que consiste em 26 letras em inglês^[a-za-z]+$
*/
public static final string str_eng = "^[a-za-z]+$";
/**
* Filtre a regularidade de string especial
*regex = "[` ~!@#$%^&*()+= | {} ':;', // [//]. <>/? ~!@#¥%… &*() - +| {} 【】 ';: "'.,?]";
*/
public static final string str_special = "[` ~!@#$%^& *()+= | {} ':;', // [//]. <>/? ~!@#¥%… & * () -+| {} 【】 ';: ""'.
/***
* Data de suporte regular:
* AAAA-MM-DD
* Aaaa/mm/dd
* Yyyy_mm_dd
* Yyyymmdd
* Forma de AAAA.
*/
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) $)) ";
/***
* Data de suporte regular:
* AAAA-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) ";
/**
* Expressão regular de URL
* Combine 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*)*) $";
/**
* Expressão regular do cartão de identificação
*/
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})";
/**
* Código institucional
*/
public static final string jigou_code = "^[a-z0-9] {8}-[a-z0-9] $";
/**
* Combine a string ^[0-9]+$
*/
public static final string str_num = "^[0-9]+$";
//// ------------------------------------------------ -------------------------------------------------------- -----------------------------
/**
* Determine se o campo está vazio e corresponde à tura de retorno
* @param str
* @return boolean
*/
public static sincronizado booleano strisnull (string str) {
Retorno NULL == STR || STR.TRIM (). Length () <= 0?
}
/**
* Devolver a termo se o campo não estiver vazio
* @param str
* @return boolean
*/
public estático booleano strnotNull (string str) {
retornar! strisnull (str);
}
/**
* String nula para esvaziar
* @param str
* @return boolean
*/
public static string nulnostr (string str) {
retornar strisnull (str)? "": str;
}
/**
* String NULL attmentation Valor padrão
* @param str string de destino
* Valor padrão @Param Defaut
* @return string
*/
public static string nulnostr (string str, string Defaut) {
retornar strisnull (str)?
}
/**
* Determinar se o campo está email e retorna a Ture
* @param str
* @return boolean
*/
public static boolean isEmail (string str) {
retornar regularmente (str, email);
}
/**
* Determine se o número de telefone atende à tura de retorno
* @param str
* @return boolean
*/
public estático booleano isphone (string str) {
retornar regular (STR, telefone);
}
/**
* Determine se o número do telefone celular atende ao status
* @param str
* @return boolean
*/
public estático booleano ismobile (string str) {
retornar regular (STR, Mobile);
}
/**
* Determinar se é URL e retorna a Ture
* @param str
* @return boolean
*/
public estático booleano isurl (string str) {
retornar regularmente (STR, URL);
}
/**
* Determinar se o campo é um número, número inteiro positivo e negativo, número de ponto flutuante positivo e negativo, retorne a tura
* @param str
* @return boolean
*/
public static boolean isNumber (string str) {
retornar regular (STR, duplo);
}
/**
* Determinar se o campo é inteiro e retorna
* @param str
* @return boolean
*/
public estático booleano isinteger (string str) {
retornar regularmente (str, número inteiro);
}
/**
* Determine se o campo é uma expressão regular positiva> = 0 e corresponde à tura de retorno
* @param str
* @return boolean
*/
public estático booleano isinteger_negative (string str) {
retornar regularmente (str, Integer_negative);
}
/**
* Determine se o campo é uma expressão regular negativa <= 0 e corresponde à tura de retorno
* @param str
* @return boolean
*/
public estático booleano isinteger_positivo (string str) {
retornar regularmente (str, Integer_positive);
}
/**
* Determinar se o campo é o dobro e retorna a tura
* @param str
* @return boolean
*/
public estático booleano isdouble (string str) {
retornar regular (STR, duplo);
}
/**
* Determine se o campo é um número de ponto flutuante positivo Expressão regular> = 0 para corresponder à tura de retorno
* @param str
* @return boolean
*/
public estático booleano isdouble_negative (string str) {
retornar regularmente (str, duplo_negativo);
}
/**
* Determine se o campo é um número de ponto flutuante negativo expressão regular <= 0 e corresponde à Ture Return Ture
* @param str
* @return boolean
*/
public estático booleano isdouble_positivo (string str) {
retornar regularmente (str, duplo_positivo);
}
/**
* Determinar se o campo é datado e retorna
* @param str
* @return boolean
*/
public static boolean iSdate (string str) {
retornar regularmente (str, date_all);
}
/**
* Verificação 2010-12-10
* @param str
* @retornar
*/
public estático booleano ISDATE1 (String str) {
retornar regularmente (str, date_format1);
}
/**
* Retorne à terrivelmente se o campo for compatível com a idade
* @param str
* @return boolean
*/
public static boolean isage (string str) {
retornar regularmente (STR, idade);
}
/**
* Determinar se o campo é muito longo
* Se a string estiver vazia, ele retornará Fasle, se exceder o comprimento {Leng}, ele retornará Ture e se retornar
* @param str
* @param leng
* @return boolean
*/
public estático booleano islengout (string str, int length) {
retornar strisnull (str)? false: str.Trim (). comprimento ()> comprimento;
}
/**
* Determinar se o campo é um cartão de identificação e retorna
* @param str
* @return boolean
*/
public static boolean Isidcard (String str) {
if (strisnull (str)) retorna false;
if (str.Trim (). Length () == 15 || str.Trim (). Length () == 18) {
retornar regularmente (STR, IDCARD);
}outro {
retornar falso;
}
}
/**
* Determine se o campo tem um código postal que corresponde à tura de retorno
* @param str
* @return boolean
*/
public static boolean iscode (string str) {
retornar regularmente (str, código);
}
/**
* Determine se todas as cordas são letras em inglês
* @param str
* @return boolean
*/
public estático booleano isenglh (string str) {
retornar regularmente (str, str_eng);
}
/**
* Determine se todas as cordas são letras em inglês + números
* @param str
* @return boolean
*/
public static boolean iseng_num (string str) {
retornar regularmente (str, str_eng_num);
}
/**
* Determine se todas as cordas são letras em inglês + números + sublinhamentos
* @param str
* @return boolean
*/
public static boolean iseng_num_ (string str) {
retornar regular (str, str_eng_num_);
}
/**
* Filtre as cordas especiais retornam strings filtrados
* @param str
* @return boolean
*/
public static string filterstr (string str) {
Padrão p = padrão.compile (str_special);
Matcher M = P.Matcher (STR);
retornar M.Replaceall (""). Trim ();
}
/**
* Verifique o formato do código do mecanismo
* @retornar
*/
public estático booleano isjigoucode (string str) {
retornar regularmente (str, jigou_code);
}
/**
* Determinar se a string é composta de números
* @param str
* @return boolean
*/
public static boolean Isstr_num (String str) {
retornar regularmente (str, str_num);
}
/**
* Se a correspondência está em conformidade com o padrão de expressão regular
* @param str com string de correspondência
* @param padrão de correspondência padrão
* @return boolean
*/
Private estático booleano regular (String str, String Pattern) {
if (null == str || str.Trim (). comprimento () <= 0)
retornar falso;
Padrão p = padrão.compile (padrão);
Matcher M = P.Matcher (STR);
retornar m.matches ();
}
}
O exposto acima é tudo sobre este artigo, espero que gostem.