正規式を使用して電話番号、ID番号、日付形式、URL、電子メール、その他の形式を確認するツールクラスを共有する
コードコピーは次のとおりです。
パッケージcom.eabax.util;
java.util.regex.matcherをインポートします。
java.util.regex.patternをインポートします。
/**
*検証ツールクラス
* @author管理者
*
*/
パブリッククラスの検証{
// ----------------------------------------------------------- -------------------------------------------------------------- ----------------------------------
/**
*正規表現= "^([a-z0-9a-z]+[ - | //。]?)+[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-z ]+( - [a-z0-9a-z]+)?//。)+[a-za-z] {2、} $ ";;
public static final string email = "//w+(/.//w+)*//w+(/.//w+)+";
/**
*電話番号正規表現=(^(/d {2,4} [-_----]?)?/d {3,8}([-_-----]?/d {3、 8})?([-_-]?/d {1,7})?
*/
public static final string phone = "(^(// d {2,4} [-_---]?)?// d {3,8}([-_----]?// d {3,8})?([-_---]?// d {1,7})?$)|(^0?1 [35] // d {9} $) ";
/**
*携帯電話番号正規表現=^(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} $";
/**
*整数正規表現 ^ - ?(([1-9]/d* $)| 0)
*/
public static final string integer = "^ - ?(([1-9] // d*$)| 0)";
/**
*正の整数正規表現> = 0 ^[1-9]/d* | 0 $
*/
public static final string integer_negive = "^[1-9] // d*| 0 $";
/**
*負の整数正規表現<= 0 ^ - [1-9]/d* | 0 $
*/
public static final string integer_positive = "^ - [1-9] // d*| 0 $";
/**
*二重正規表現 ^ - ?([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)$ ";
/**
*二重正規表現> = 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 $ ";
/**
*負の二重正規表現<= 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$ ";
/**
*年齢正式な表現^(?:[1-9] [0-9]?| 1 [01] [0-9] | 120)$ 0-120歳の一致
*/
public static final string age = "^(?:[1-9] [0-9]?| 1 [01] [0-9] | 120)$";
/**
*郵便番号正規表現[0-9]/d {5}(?!/d)国内6桁の郵便番号
*/
public static final string code = "[0-9] // d {5}(?!// d)";
/**
*数字、26の英語の文字またはアンダースコアで構成される文字列を一致させる ^/w+$
*/
public static final string str_eng_num _ = "^// w+$";
/**
*数字と26の英語文字で構成される文字列を一致させます^[a-za-z0-9]+$
*/
public static final string str_eng_num = "^[a-za-z0-9]+";
/**
* 26の英語文字^[a-za-z]+$で構成される文字列に一致します
*/
public static final string str_eng = "^[a-za-z]+$";
/**
*特別な文字列の規則性をフィルタリングします
*regex = "[`〜!@#$%^&*()+= | {} ':;'、// [//]。<>/?〜!@#¥%…&*() - +| {}【】 ';: "'。、?]";
*/
public static final string str_special = "[`〜!@#$%^& *()+= | {} ':;'、// [//]。<>/?〜!@#¥%…& * () - +| {} ';: "'。
/***
*定期的なサポートの日付:
* yyyy-mm-dd
* yyyy/mm/dd
* yyyy_mm_dd
* yyyymmdd
* 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)([ - /////._]? 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)$)) ";
/***
*定期的なサポートの日付:
* 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-)) - ((0 13578] | 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 [48] | [ 2468] [048] | [13579] [26])|((0 [48] | [2468] [048] | [3579] [26])00)) - 02-29) ";
/**
* URL正規表現
* http www ftpを一致させます
*/
public static final string url = "^(http | www | ftp |)?(://)?(// w+( - // w+)*)(//。 )*((:// d+)?)(/(// w+( - // w+)*))*(//。?(// w)*)(//?)(//?) ? " +
((((((//w* %)*///w*//?(//w* :)*」) (// w*&)*(// w* - )*(// w*=)*(// w*%)*(// w*//?)*" +
"(//w*:///w*//+)*(//w*//.)*" +
"(// w*&)*(// w* - )*(// w*=)*(// w*)*)$";
/**
* IDカードの正規表現
*/
public static final string idcard = "(((11 | 12 | 13 | 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}) ";
/**
*制度コード
*/
public static final string jigou_code = "^[a-z0-9] {8} - [a-z0-9] $";
/**
*文字列 ^[0-9]+$を一致させます
*/
public static final string str_num = "^[0-9]+$";
///----------------------------------------------------------------------------------- -------------------------------------------------------------- ----------------------------------
/**
*フィールドが空であるかどうかを判断し、リターンチュールに一致します
* @param str
* @return boolean
*/
public static同期ブールンstrisnull(string str){
null == str.trim()<= 0?
}
/**
*フィールドが空でない場合は、tureを返します
* @param str
* @return boolean
*/
public static boolean strnotnull(string str){
return!strisnull(str);
}
/**
* string nullから空になります
* @param str
* @return boolean
*/
public static string nulltostr(string str){
return strisnull(str)? "":str;
}
/**
*文字列null割り当てデフォルト値
* @param strターゲット文字列
* @param defautデフォルト値
* @return文字列
*/
public static string nulltostr(string str、string defaut){
return strisnull(str)?defaut:str;
}
/**
*フィールドが電子メールであるかどうかを判断し、tureを返します
* @param str
* @return boolean
*/
public static boolean isemail(string str){
レギュラー(str、email)を返します。
}
/**
*電話番号が返品を満たしているかどうかを判断します
* @param str
* @return boolean
*/
public static boolean isphone(string str){
レギュラー(str、電話)を返します。
}
/**
*携帯電話番号がステータスを満たしているかどうかを判断します
* @param str
* @return boolean
*/
public static boolean ismobile(string str){
レギュラー(str、モバイル)を返します。
}
/**
*それがURLであるかどうかを判断し、tureを返します
* @param str
* @return boolean
*/
public static boolean isurl(string str){
レギュラー(str、url)を返します。
}
/**
*フィールドが数、正と負の整数、正と負の浮動点数であるかどうかを判断し、tureを返します
* @param str
* @return boolean
*/
public static boolean isnumber(string str){
レギュラー(str、double)を返します。
}
/**
*フィールドが整数であるかどうかを判断し、tureを返します
* @param str
* @return boolean
*/
public static boolean isinteger(string str){
レギュラー(str、整数)を返します。
}
/**
*フィールドが正の正規表現であるかどうかを判断します> = 0
* @param str
* @return boolean
*/
public static boolean isinteger_negative(string str){
RETURN REMULTION(str、integer_negative);
}
/**
*フィールドが負の整数正規表現であるかどうかを判断します<= 0
* @param str
* @return boolean
*/
public static boolean isinteger_positive(string str){
RETURN REMULTION(STR、integer_Positive);
}
/**
*フィールドが二重であるかどうかを判断し、tureを返します
* @param str
* @return boolean
*/
public static boolean isdouble(string str){
レギュラー(str、double)を返します。
}
/**
*フィールドが正の浮動小数点数の正規表現> = 0であるかどうかを判断します。
* @param str
* @return boolean
*/
public static boolean isdouble_negative(string str){
レギュラー(str、double_negative)を返します。
}
/**
*フィールドが負の浮動小数点数の正規表現<= 0であるかどうかを判断し、リターンチュールに一致します
* @param str
* @return boolean
*/
public static boolean isdouble_positive(string str){
レギュラー(str、double_positive)を返します。
}
/**
*フィールドが日付が付けられているかどうかを判断し、tureを返します
* @param str
* @return boolean
*/
public static boolean isdate(string str){
RETURN REMULTION(str、date_all);
}
/**
*検証2010-12-10
* @param str
* @戻る
*/
public static boolean isdate1(string str){
return remulal(str、date_format1);
}
/**
*フィールドが年齢に合わせている場合は、tureに戻ります
* @param str
* @return boolean
*/
public static boolean isage(string str){
レギュラー(str、age)を返します。
}
/**
*フィールドが長すぎるかどうかを判断します
*文字列が空の場合、それはfasleを返し、長さ{leng}を超えている場合、それはtureを返し、falseを返す場合
* @param str
* @param leng
* @return boolean
*/
public static boolean islengout(string str、int length){
return strisnull(str)?false:str.trim()。length()> length;
}
/**
*フィールドがIDカードであり、tureを返すかどうかを判断します
* @param str
* @return boolean
*/
public static boolean isidcard(string str){
if(strisnull(str))falseを返します。
if(str.trim()。length()== 15 || str.trim()。length()== 18){
レギュラー(str、idcard)を返します。
}それ以外 {
falseを返します。
}
}
/**
*フィールドに戻り値に一致する郵便番号があるかどうかを判断します
* @param str
* @return boolean
*/
public static boolean iscode(string str){
レギュラー(str、code)を返します。
}
/**
*すべての文字列が英語の文字であるかどうかを判断します
* @param str
* @return boolean
*/
public static boolean isenglish(string str){
RETURN REMULTION(STR、STR_ENG);
}
/**
*すべての文字列が英語の文字 +番号かどうかを判断します
* @param str
* @return boolean
*/
public static boolean iseng_num(string str){
return remulal(str、str_eng_num);
}
/**
*すべての文字列が英語の文字 +番号 +アンダースコアかどうかを判断します
* @param str
* @return boolean
*/
public static boolean iseng_num_(string str){
return remulal(str、str_eng_num_);
}
/**
*フィルター特別な文字列ろ過されたろ過された文字列を返します
* @param str
* @return boolean
*/
public static string filterstr(string str){
パターンP = pattern.compile(str_special);
Matcher M = P.Matcher(str);
m.ReplaceAll( "")。トリム()を返します。
}
/**
*メカニズムのコード形式を確認します
* @戻る
*/
public static boolean isjigoucode(string str){
レギュラー(str、jigou_code)を返します。
}
/**
*文字列が数値で構成されているかどうかを判断します
* @param str
* @return boolean
*/
public static boolean isstr_num(string str){
レギュラー(str、str_num)を返します。
}
/**
*一致が正規表現パターンに準拠しているかどうか
* @param strマッチング文字列
* @paramパターンマッチングパターン
* @return boolean
*/
Private Static Boolean Regular(String str、string pattern){
if(null == str || str.trim()。length()<= 0)
falseを返します。
パターンP = pattern.compile(pattern);
Matcher M = P.Matcher(str);
m.matches()を返します。
}
}
上記はこの記事のすべてです。あなたがそれを気に入ってくれることを願っています。