ImportJava.util.regex.Matcher; ImportJava.util.regex.Pattern; Importjava.util.regex.PatternSyntaxException; Public classPhoneformatcheckutils {/*** Mainland or Hong Kong numbers are available*/ Public Static BooleanIsphonelegal (String str)throwspatternsyntaxexception {returnIschinaphonelegal (str) | |Ishkphonelegal (str); } /*** Mainland Mobile phone number 11 digits, match format: first three bit fixed format + 8 digits any number * This method in the first three bit format has: * 13+ any number * 15+ any number except 4 * + any number except 1 and 4 * 17+ Any number except 9 * 147*/ Public Static BooleanIschinaphonelegal (String str)throwspatternsyntaxexception {String regExp= "^ ((13[0-9]) | (15[^4]) | (18[0,2,3,5-9]) | (17[0-8]) | (147)) \\d{8}$ "; Pattern P=Pattern.compile (REGEXP); Matcher m=P.matcher (str); returnm.matches (); } /*** Hong Kong mobile phone number 8 digits, 5|6|8| 9 Opening + 7-bit arbitrary number*/ Public Static BooleanIshkphonelegal (String str)throwspatternsyntaxexception {String regExp= "^ (5|6|8| 9) \\d{7}$ "; Pattern P=Pattern.compile (REGEXP); Matcher m=P.matcher (str); returnm.matches (); } }
Java Regular Expression notes