정규 표현식은 종종 다양한 형태를 검증하는 데 사용됩니다. Java Form 등록은 일반적으로 정규식 검증 도구 클래스에 일반적으로 사용되며, 정규식의 대규모 컬렉션은 일반적으로 사용됩니다.
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;/** * @project baidamei * @author ceencheng <[email protected]. 4:54:42 pm */public class regexutils {/** * 이메일 확인 * @param 이메일 이메일 주소, 형식 : [email protected], [email protected], xxx는 Verification이 성공할 경우 이메일 서비스 제공자를 나타냅니다. "//w+@//w+//.9 Z ]+(//.9];"; "; 반환 Pattern.matches (Regex, 이메일); }/*** ID 카드 번호 확인* @param idcard 거주 ID 카드 번호 15 또는 18, 마지막 숫자는 숫자 또는 문자 일 수 있습니다. 반환 Pattern.matches (Regex, Idcard); } /*** 휴대 전화 번호 확인 (국제 형식 지원, +86135xxxx ... (TD 네트워크 카드에 사용될 것으로 예상) *, 150, 151, 152, 157 (TD- 특이 적), 158, 159, 187 (활성화되지 않음), 188 (TD- 특이 적) </p> *<p> China Unicom의 숫자 세그먼트 : 130, 131, 132, 155, 156 (World-Wind-Specific), 185 (3), 186 (3), 186 (3). *<p> Telecom의 번호 세그먼트 : 133, 153, 180 (활성화되지 않음), 189 </p> *@return verification이 성공적이며 참으로 반환되고, 검증이 실패하고 false */public static boolean checkmobile (String Mobile) {String Regex = "(//+// d+)? 1 [34578] // d} $"; 반환 Pattern.matches (Regex, Mobile); } /*** 유선 번호 확인* @Param 전화 번호, 형식 : 국가 (지역) 전화 코드 + 지역 코드 (도시 코드) + 전화 번호 : +8602085558847* <p> <b> 국가 (지역) 코드 : < /b> 전화 번호의 국가 (지역)를 식별하는 표준 국가 (지역) 코드. 여기에는 0에서 9까지의 하나 이상의 숫자가 포함되며 우주 분리 국가 코드가 포함됩니다. </p> * <p> <b> 지역 코드 (도시 코드) : </b> 여기에는 0에서 9까지의 숫자가 하나 이상 포함될 수 있으며, 지역이나 도시 코드는 지역이나 도시 코드를 사용하지 않는 국가 (지역)의 경우 괄호 안에 배치됩니다.이 구성 요소는 생략됩니다. </p> * <p> <b> 전화 번호 : </b> 여기에는 0 ~ 9 </p> * @return이 확인이 성공할 때 true를 반환하고 확인이 실패하면 false를 반환합니다 */public static boolean checkphone (String Phone) {String regex = "(//+// d+)? 반환 Pattern.matches (Regex, Phone); }/** * 정수를 확인하십시오 (양수 정수 및 음성 정수) * @param 숫자는 하나 이상의 비트 0-9 사이의 @param 숫자 정수 */public static boolean checkDigit (String Digit) {String regex = "//? [1-9] // d+"; 반환 Pattern.matches (REGEX, DIGIT); } / ** * 정수 및 부동 소수점 번호 (양수 및 부정적인 정수 및 양수 및 부정적인 부동산 숫자) * @Param Decimals 1 개 이상의 비트 0-9 사이의 플로팅 포인트 숫자 : 1.23, 233.30 * @return 성공적인 검증 후 true, false * / public static boolean checkdecimals (string decimals) {string Regex) "//-? [1-9 ]//d+(//./d+)?"; 반환 Pattern.matches (Regex, Decimals); } /** * whitespace 문자 확인 * @param blankspace whitespace 문자 : space, /t, /n, /r, /f, /x0b * @return 확인이 성공할 때 true를 반환하고 확인이 실패 할 때 False를 반환합니다 * /public static boolean checkblankspace (String blankspace) {String Regex = "// s+"; 반환 Pattern.matches (Regex, BlankSpace); }/*** 중국어를 확인하십시오* @param 중국어 중국어* @return 확인이 성공할 때 true를 반환하고 확인이 실패 할 때 False를 반환합니다.*/public static boolean checkinese (string cheence) {String regex = "^[/u4e00-/u9fa5]+$"; 반환 Pattern.matches (Regex, Chinese); } / ** * 확인 날짜 (연도, 월 및 날짜) * @param 생일 날짜, 형식 : 1992-09-03 또는 1992.09.03 * @return 성공적인 검증 후 true, false vencification * / public static boolean checkbirthday (string regex =) {String Regex = "[1-9] {4} ([-./]) // d {1,2} // 1 // d {1,2}"; 반환 Pattern.matches (Regex, Birthday); }/*** 확인 URL 주소* @Param URL 형식 : http://blog.csdn.net:80/xyang81/article/details/7705960? 또는 http://www.csdn.net:80 * @return 성공적인 확인 후 true, false vencifice */public static boolean checkurl (String url) {String regex = "(https? : // (w {3} //)?)? // w+//. // w+(//. 반환 Pattern.matches (REGEX, URL); }/** * <fre> * URL URL의 첫 번째 레벨 도메인을 가져옵니다 * </pre> * * @param url * @return */public static string getDomain (string url) {pattern p = pattern.compile ( "(? <= http : // //?] *? // (com | cn | kn | viz | viz | cc | tv). pattern.case_insensive); // 전체 도메인 이름을 가져옵니다 // 패턴 p = pattern.comPile ( "[^//]*? //. 매치 업체 매치 자 = p.matcher (URL); matcher.find (); return matcher.group (); }/** * 일치 중국 우편 번호를 일치시킵니다 * @param 우편 번호 우편 번호 * @return return return return return return return return return return return return return return return return returny vencification */public static boolean checkpostcode (string postcode) {string regex = "[1-9] // d {5}"; 반환 Pattern.matches (Regex, 우편 번호); } / ** * 일치 IP 주소 (간단한 일치, 형식, 예 : 192.168.1.1, 127.0.0.1, IP 세그먼트의 일치 크기 없음) * @param iPaddress IPv4 표준 주소 * @return은 성공적인 확인 후 true를 반환합니다. "[1-9] (// d {1,2})? //. (0 | ([1-9] (// d {1,2})))) //. 1,2})))) //. (0 | ([1-9] (// d {1,2})?)) //. 반환 Pattern.matches (Regex, iPaddress); }}
정규 표현식을 사용하여 전화 번호, ID 번호, 날짜 형식, URL, 이메일 및 기타 형식을 확인하는 도구 클래스 공유
패키지 com.eabax.util; import java.util.regex.matcher; java.util.regex.pattern import; /** * 검증 도구 클래스 * @author admin */public class 검증 { //--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- "^([a-z0-9a-z]+[-| //]?)+[a-z0-9a-z]@([a-z0-9a-z]+(-[a-z0-9a-z]+)+)+[a-za-z] {2,} $"; 공개 정적 최종 문자열 이메일 = "//w+(//./w+)*@/wfle(//./w+); /** * 전화 번호 정규 표현 = (^(/d {2,4} [-_--]?)?)?)?)?)?/d {3,8} ([-_---]?/d {3,8})? ([-_---]?/d {3,8})? ([-_---]?/d {1,7}) | 공개 정적 최종 문자열 전화 = "(// d {2,4} [-_--]?)? // d {3,8} ([-_---]? // d {3,8})? ([-_---]? // d {3,8})? ([-_---]? // d {1,7}) | /** * 휴대 전화 번호 정규 표현 =^(13 [0-9] | 14 [0-9] | 15 [0-9] | 17 [0-9] | 18 [0-9])) // d {8} $ */public static final String mobile = "^(13] | 14 [0-9] | 15 [0-9] | 17 [0-9] | 0-9]//d} $. /***정수 정규 표현식^-? (([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 $"; /***이중 정규식^-? /***이중 정규식> = 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] // 0 | 0 | 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) $ 1CET 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? "| (^((1 [8-9] // d {2}) | ([2-9] // d {3})) ([-//// ._]?) (11 | 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) $) | ([2-9] [0-9] [0] [48]) ([-//// ._]?)" + "(0? 2) ([-///// ._]?) (29) $)" + "| (^([1] [89] [2468] [048]) ([-////// ._]?) (0? 2) ([-///// ._]?) (29) $) | ([2-9] [0-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-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 [0 [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*/??)* (/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 | 34 | 35 | 36 | 37 | 41 | 42 | 43 | 44 | 45 | 46 | 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] {3}" + "[xx0-9]) | ([0-9] {2} [0-9] [0-3] [0-3] [0-9] {3}); "^[a-z0-9] {8}-[a-z0-9] $"; / ** * 숫자와 일치하는 문자열^[0-9]+$ * / public static final String str_num = "^[0-9]+$"; ////--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Determine that the field is 무감각하고 법을 준수합니다. ture * @param str * @return boolean */ public static boolean strnotnull (String str) {return! strisnull (str); } / ** * string null to null * @param str * @return boolean * / public static string nulltostr (String str) {return strisnull (str)? "": str; } / *** 문자열 null 할당 기본값* @param str target string* @param defaut default value* @return string* / public static string nulltostr (String Str, String defaut) {return strisnull (str)? defaut : str; } / ** * 필드가 이메일이며 일치하는지를 결정하십시오. } / ** * 전화 번호가 법률을 충족하는지 여부를 결정하십시오 * @param str * @return boolean * / public static boolean isphone (string str) {return regular (str, phone); } / ** * 휴대 전화 번호가 상태와 일치하는지 여부를 결정하십시오 * @param str * @return boolean * / public static boolean ismobile (String Str) {return Regular (str, mobile); } / ** * 그것이 URL 호환 여부를 결정하십시오. return that that that that a @param st * @return boolean * / public static boolean isurl (string str) {return regular (str, url); } / ** * 필드가 숫자의 긍정적이고 음의 정수인지 결정하십시오. 양수 및 음의 부동물 포인트 번호는 상태와 일치합니다 * @param str * @return boolean * / public static boolean isnumber (String Str) {reture Regular (str, double); } / ** * 필드가 정수 호환되는지 여부를 결정하십시오. } / ** * 필드가 긍정적 인 정수 정규 표현인지 여부를 결정하십시오.> = 0 자연을 반환하기 위해 일치합니다 * @param st * @return boolean * / public static boolean isinteger_negative (String str) {return exular (str, integer_negative); } / ** * 필드가 음의 정수 정규 표현인지 <= 0인지 결정하고 자연을 반환합니다 * @param st * @return boolean * / public static boolean isinteger_positive (String str) {reture (str, integer_positive); } / ** * 필드가 두 배 의지 여부를 결정하고 자연을 반환합니다 * @param str * @return boolean * / public static boolean isdouble (String Str) {retuderal (str, double); } / ** * 필드가 긍정적 인 부동물 지점 정규 표현인지 여부를 결정하십시오.> = 0 자연을 반환하기 위해 * @param st * @return boolean * / public static boolean isdouble_negative (String Str) {return regular (str, double_negative); } / ** * 필드가 네거티브 플로팅 포인트인지 판단 정규 표현식 <= 0 자연을 반환하기 위해 일치합니다 * @param sth * @return boolean * / public static boolean isdouble_positive (String Str) {reture Regular (str, double_positive); } / ** * 필드가 날짜와 호환되는지 여부를 결정할 때 자연을 반환하십시오 * @param str * @return boolean * / public static boolean isdate (String str) {retuderal (str, date_all); } / ** * 검증 2010-12-10 * @param str * @return * / public static boolean isdate1 (string str) {return regular (str, date_format1); } / ** * 필드가 연령과 호환되는지 여부를 결정할 때 자연을 반환합니다 * @param st * @return boolean * / public static boolean isage (String str) {return Regular (str, age); } /** * 필드가 너무 길어지는 지 확인 * 문자열이 비어 있으면 페일을 반환하고 길이 {leng}을 초과하면 대신 자연을 반환합니다. return false * @param str * @param length * @return boolean */ public static boolean islengout (String str, int length) {return strisnull (str)? false : str.trim (). length ()> 길이; } / ** * 필드가 ID 카드인지 확인하고 자연을 반환합니다. } else {return false; }} / ** * 필드에 특성과 일치하는 우편 번호가 있는지 여부를 결정하십시오 * @param sth * @return boolean * / public static boolean iscode (String Str) {return 일반 (str, code); } / ** * 문자열이 모두 영어 문자 여부를 결정하십시오 * @param str * @return boolean * / public static boolean isenglish (String str) {reture Regular (str, str_eng); } / ** * 문자열이 영어 문자 + 숫자 * @param str * @return boolean * / public static boolean iseng_num (String str) {return regular (str_eng_num); } / ** * 문자열이 영어 문자 + 숫자 + 밑줄이 모두 여부를 결정합니다 * @param st * @return boolean * / public static boolean iseng_num_ (String str) {return regular (str, str_eng_num_); } / ** * 필터 특수 문자열 필터링 문자열을 반환합니다. 매치 자 m = p.matcher (str); M.Replaceall ( ""). trim (); } / *** 메커니즘 코드 형식을 확인하십시오* @return* / public static boolean isjigoucode (String str) {return regular (str, jigou_code); } / ** * 문자열이 숫자로 구성되어 있는지 여부를 결정하십시오 * @param str * @return boolean * / public static boolean isst_num (String str) {reture Regular (str, str_num); } / *** 경기가 정규 표현식 패턴 일치를 충족하는지 여부* @param str 매칭 문자열* @param 패턴 일치 패턴* @return boolean* / private static boolean 일반 (문자열 str, 문자열 패턴) {if (null == str || str.trim (). length () <= 0) 거짓; Pattern P = Pattern.compile (Pattern); 매치 자 m = p.matcher (str); return m.matches (); }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.