/*** Check if the email address is legal *@paramEmail *@returntrue Legal false not valid*/ Importjava.util.regex.*;//a processing class that provides regular expressions in the package Public classhello{ Public Static voidMain (String[]args) {String email= "[Email protected]"; SYSTEM.OUT.PRINTLN (email); Validate (email); }Private Static BooleanValidate (String email) {BooleanIsexist =false; /*in the regular expression \w means that any single character range is a-z,a-z,0-9, because in Java \ is an escape symbol, if only write as \w will be ambiguous, or even error, so write as: \\w+ means to appear more than once, so \\w+ Represents a string of any length, but does not include other special characters such as _,-, $,&,*, etc.*///(\\w+.) + indicates that the server may have a multilevel domain name, [a-z]{2,3} indicates a maximum of 2-3 domain names. /*Pattern p = pattern.compile ("\\[email protected" (\\w+.) +[a-z]{2,3} "); Matcher m = p.matcher (email); Boolean B = m.matches (); *///The first method of presentation if((Pattern.matches ("\\[email protected") (\\w+.) +[a-z]{2,3} ", email))) {System.out.println ("Valid email address"); Isexist=true; } Else{System.out.println ("Invalid email address"); } returnisexist; } }
Java to determine whether email is legitimate