多くの場合、通常の式は、さまざまな形式を検証するために使用されます。 Javaフォーム登録は、一般に正規表現検証ツールクラスに使用され、正規表現の大規模なコレクションが一般的に使用されます。
1。電話番号
2。郵便番号
3。QQ
4。電子メール
5。携帯電話番号
6。URL
7。それは数字ですか
8。それは中国人ですか?
9。身分証明書
10。ドメイン名
11。IP。 。 。 。
すべて一般的な検証があります!これは確かに、あなたがウェブ開発とサーバー側のフォーム検証に従事する良い製品です!あなたは ^_ ^に値する
/ * * Copyright 2012-2013 The Haohui Network Corporation */Package com.haohui.common.utils; Import java.util.regex.matcher; Import java.util.regex.pattern; 4:54:42 PM */public class regexutils {/** *電子メールの検証 * @paramメールアドレス、フォーマット:[email protected]、[email protected]、xxxは電子メールサービスプロバイダーを表します * @return戻り値が正常である場合、string fails fails */public static boowean "//w+@//w+//. return pattern.matches(regex、email); }/*** IDカード番号を検証しますreturn pattern.matches(regex、idcard); } /***携帯電話番号を検証します(国際形式、 +86135xxxx ...(中国本土)、 +00852137xxxx ...(香港、中国)) (TDネットワークカードに使用されると予測) *、150、151、152、157(TD固有)、158、159、187(有効化されていない)、188(TD固有)</p> *<p>中国ユニコムの番号セグメント:130、131、132、155、156(世界 - 特異的)、185(3g) *<p> Telecomの番号セグメント:133、153、180(有効化されていない)、189 </p> *@return検証は成功し、確認されます。 return pattern.matches(regex、mobile); } /***固定電話番号を確認します* @param電話番号、フォーマット:国(地域)電話コード +市外局所コード(都市コード) +電話番号:+860208558588447* <p> <b>国コード:< /b>電話番号を識別する標準国(地域)コード。 0から9までの1桁以上が含まれており、その後にスペース分離された国コードが続きます。 </p> * <p> <b>市外局番(都市コード):</b>これには0から9までの1つ以上の数字が含まれ、地域または都市コードは括弧に配置されます - *地域または都市コードを使用しない国(地域)の場合、このコンポーネントは省略されています。 </p> * <p> <b>電話番号:</b>これには0から9の1つ以上の数値が含まれています</p> * @return検証が成功した場合、確認が失敗した場合に確認を返します */public static boolean checkphone(string regex = "(//+// d+)? return pattern.matches(regex、電話); }/** *検証が成功した場合、整数(正の整数と負の整数)を検証します * 1つまたは複数のビットの間に0-9パブリックstatic boolean checkdigit(string digit){string regex = "// - ?[1-9] // d+"; return pattern.matches(regex、gigit); } / ** *整数と浮動小数点数(正と負の整数と正と負の浮動小数点数) * @param Decimals @param Decimals 1つ以上のビットの間の浮動小数点0-9の間、1.23、233.30 * @return Return True、故障確認"//-?[1-9] return pattern.matches(regex、decimals); } /** * Whitespace文字を検証します * @param Blankspace Whitespace文字、スペース、 /t、 /n、 /r、 /f、 /x0b * @return検証が成功したときにtrueを返します。 return pattern.matches(regex、blankspace); }/***中国語の検証* @param中国語文字* @return return return return return return return return return return return return return return with verification faileが失敗したときにfalseを返します*/public static boolean checkchinese(string estrine){string regex = "^[/u4e00--/u9fa5]+$"; return pattern.matches(regex、中国語); } / ** *検証日(年、月、日付) * @param誕生日の日付、形式:1992-09-03、または1992.09-03、@returnは、成功した検証後に真の復帰、故障後の虚偽 * / public static boolean checkbirthday(string Birthday){string regex = "[1-9] {4}([ - 。/])// d {1,2} // 1 // d {1,2}"; return pattern.matches(regex、Birthday); }/***検証urlアドレス* @param urlフォーマット:http://blog.csdn.net:80/xyang81/article/details/7705960?またはhttp://www.csdn.net:80 * @return return return rutle return rutle rutch rutch engification fails false false affering fildification */public static boolean checkurl(string url){string regex = "(https?://(w {3} //。)?)?// w+//。// w+(//。[a-za-z]+)*(:// d {1,5})?(/// w*)*)*(// ??(。+=。*)?(&。+ return pattern.matches(regex、url); }/** * <pre> * url urlの最初のレベルのドメインを取得 * </pre> * * @param url * @return */public static string getdomain(string url){pattern p = pattern.compile( "(?<= http:// | //。)[^。] *? pattern.case_insensitive); //完全なドメイン名を取得//パターンp = pattern.compile( "[^//]*?//。 matcher matcher = p.matcher(url); matcher.find(); return matcher.group(); }/** *中国の郵便番号を一致 * @param郵便番号郵便番号 * @return return true rutce rutce runty fals fals false false */public static boolean checkpostcode(string postcode){string regex = "[1-9] [1-9] // d {5}"; return pattern.matches(regex、郵便番号); } / ** *一致IPアドレス(192.168.1.1、127.0.0.1、IPセグメントのマッチングサイズなし) * @param iPaddress IPv4標準アドレス * @return return return return return return return fals fals false dight return return return return return return return choexdress(String checkadress(String iPadress){string regadress) "[1-9](// d {1,2})?//。(0 |([1-9](// d {1,2})?))//。(0 |([1-9](// d { 1,2})?)//。(0 |([1-9](// d {1,2})?))//。 return pattern.matches(regex、iPaddress); }}正規式を使用して電話番号、ID番号、日付形式、URL、電子メール、その他の形式を確認するツールクラスを共有する
パッケージcom.eabax.util; java.util.regex.matcherをインポートします。 java.util.regex.patternをインポートします。 /** *検証ツールクラス * @author admin * */public class validation { //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ^([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} [-_---]?)? public static final string phone = "(^(// d {2,4} [-_---]?)?// d {3,8}([-_----]?// d {3,8})?([-_-----]?// d {3,8}) /** *携帯電話番号正規表現=^(13 [0-9] | 14 [0-9] | 15 [0-9] | 17 [0-9] | 18 [0-9])// d {8} $ */public static final string mobile = "^(13 [0-9] | 14 [0-9] | 15 [0-9] | 17 [0-9] | 18 [0-9] | /***整数正規表現^ - ?(([1-9]/d*$)| 0)*/public static final string integer = "^ - ?(([1-9] // d*$)| 0)"; /***正の整数正規表現> = 0 ^[1-9]/d*| 0 $*/public static final string integer_negative = " ^[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 /.// /***二重正規表現> = 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 ^( - ([[1-9]/d*/。/d*| 0/./ d*[1-9]/d*))| 0?/。 = "^( - ([1-9] // d*// / ***年齢の正規表現^(?:[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 * yyymmdd * 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 [8-9] // d {2})|([2-9] // d {3}))([ - //////._]?)(11 | 0?[469])([////._]?)(30 | [12] [0-9] | 0? |(^(((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)$)" + |(^(^([1] [89] [2468] [048])([ - /////._](0?2)([ - ///////._](29)$)|(^([2-9] [2468] [048])([ - /////._]? "([ - /////._]?)(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-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 [0 [[ 48] | [2468] [048] | [13579] [26])|((0 [48] | [2468] [048] | [3579] [26]) - 02-29) "; / *** url正規表現* http www ftp*/ public static final string url = match "^(http | www | ftp |)?(://)?(// w+( - // w+)*)(//。(// w+)*)* + "(((((//w* %)*///?)*//w*:(//w*/+)*(//w*//.)*」) "(//w* :(//w*//+)(//w*//.)*" + "(// w*&)*(// w* - )*(// w*=)*)*(// w*)*)$"; idcard = "((11 | 12 | 13 | 14 | 15 | 21 | 22 | 23 | 31 | 32 | 33 | 35 | 36 | 37 | 41 | 42 | 43 | 44 | 45 | 46 | 50 | 51 | 52 | 53 | 54 | 61 | 62 | 63 | 64 | 65) 「([1 | 2] [0-9] {3} [0 | 1] [0-9] [0-3] [0-9] {3} " +" [xx0-9])|([0-9] {2} [0 | 1] [0-9] [0-3] [0-3] [0-9] {3}) "; "^[a-z0-9] {8} - [a-z0-9] $"; ////--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Determine that the field is非ヌルと法律に準拠しています。 ture * @param str * @return boolean */ public static boolean strnotnull(string str){return!strisnull(str); } / ** * string null to empt * @param str * @return boolean * / public static string nulltostr(string str){return strisnull(str)? "":str; } / *** string null assignmentデフォルト値* @param strターゲット文字列* @param defautデフォルト値* @return string* / public static string nulltostr(string str、string defaut){return strisnull(str)?dfaut:str; } / ** *フィールドが電子メールであるかどうかを判断します。 } / ** *電話番号が法律を満たしているかどうかを判断します * @param str * @return boolean * / public static boolean isphone(string str){return remulal(str、phone); } / ** *携帯電話番号がステータスに一致するかどうかを判断します * @param str * @return boolean * / public static boolean ismobile(string str){return remulal(str、mobile); } / ** *それがurl互換性のあるものかどうかを決定します。 } / ** *フィールドが数値正と負の整数整数正と負の浮動小数点数がステータスに一致するかどうかを判断します * @param str * @return boolean * / public static boolean isNumber(string str){return remulal(str、double); } / ** *フィールドが整数互換であるかどうかを決定します * @param str * @return boolean * / public static boolean isinteger(string str){return remulal(str、integer); } / ** *フィールドが正の正規表現であるかどうかを判断します> = 0自然を返すために適合 * @param str * @return boolean * / public static boolean isinteger_negative(string str){return remulal(str、integer_negative); } / ** *フィールドが負の整数正規表現であるかどうかを判断し、自然を返します * @param str * @return boolean * / public static boolean isinteger_posive(string str){return remulal(str、integer_positive); } / ** *フィールドが二重であるかどうかを判断します * @param str * @return boolean * / public static boolean isdouble(string str){return remulal(str、double); } / ** *フィールドが正のフローティングポイント正規表現であるかどうかを決定します} / ** *フィールドがネガティブフローティングポイント正規表現であるかどうかを判断します<= 0自然を返すために適合 * @param str * @return boolean * / public static boolean isdouble_positive(string str){return remulal(str、double_positive); } / ** *フィールドが日付互換であるかどうかを判断するときに自然を返します * @param str * @return boolean * / public static boolean isdate(string str){return remulal(str、date_all); } / ** *検証2010-12-10 * @param str * @return * / public static boolean isdate1(string str){return remulal(str、date_format1); } / ** *フィールドが年齢互換性があるかどうかを判断するときに自然を返します * @param str * @return boolean * / public static boolean isage(string str){return remulal(str、age); } /** *フィールドが長すぎるかどうかを確認します *文字列が空の場合はファズルを返し、長さ{leng}を超えると、代わりに自然を返します。 false * @param str * @param length * @return boolean */ public static boolean islengout(string str、int length){return strisnull(str)?false:str.trim()。length()> length; } / ** *フィールドがIDカードであり、自然を返すかどうかを確認します * @param str * @return boolean * / public static boolean isidcard(string str){if(str.trim()== 15 || str.trim()。 } else {return false; }} / ** *フィールドが特性に一致する郵便番号を持っているかどうかを判断します * @param str * @return boolean * / public static boolean iscode(string str){return remulal(str、code); } / ** *文字列がすべて英語の文字であるかどうかを判断します * @param str * @return boolean * / public static boolean isenglish(string str){return remulal(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){pattern p = pattern.compile(str_special); Matcher M = P.Matcher(str); m.ReplaceAll( "")。トリム()を返します。 } / ***メカニズムコード形式を確認* @return* / public static boolean isjigoucode(string str){return remulal(str、jigou_code); } / ** *文字列が数字で構成されているかどうかを決定 * @param str * @return boolean * / public static boolean isstr_num(string str){return remulal(str、str_num); } / ***一致が正規表現パターンマッチを満たしているかどうか真* @param strマッチング文字列* @paramパターンマッチングパターン* @return boolean* / private static boolean remulary(string str、string pattern){if(null == str || str.trim()。パターンP = pattern.compile(pattern); Matcher M = P.Matcher(str); m.matches()を返します。 }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。