public class Test {
public static void main(String[] args) {
//电子邮件
String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher("[email protected]");
boolean isMatched = matcher.matches();
System.out.println(isMatched);
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String mail=null;
System.out.println("请输入E-Mail:");
mail=scanner.next();
Pattern pattern=Pattern.compile("\\[email protected](\\w+.)+[a-z]{2,3}");//\w表示a-z,A-Z,0-9(\\转义符)
Matcher matcher=pattern.matcher(mail);
boolean b=matcher.matches();
if (b) {
System.out.println(mail+"有效的邮箱地址!");
}else {
System.out.println(mail+"的格式错误!!");
}
}
/**
*javascript电子邮箱的合法性验证
*/
function isEmail(email)
{
var srt=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
if(srt.test(email))
{
//不合法时
return false;
}
else
{
//合法时
return true;
}
}
}
public static boolean validateEmail(String email) {
boolean flag = false;
int pos = email.indexOf("@");
if (pos == -1 || pos == 0 || pos == email.length() - 1) {
return false;
}
String[] strings = email.split("@");
if (strings.length != 2) {// 如果邮箱不是[email protected]格式
return false;
}
CharSequence cs = strings[0];
for (int i = 0; i < cs.length(); i++) {
char c = cs.charAt(i);
if (!Character.isLetter(c) && !Character.isDigit(c)) {
return false;
}
}
pos = strings[1].indexOf(".");// 如果@后面没有.,则是错误的邮箱。
if (pos == -1 || pos == 0 || pos == email.length() - 1) {
return false;
}
strings = strings[1].split(".");
for (int j = 0; j < strings.length; j++) {
cs = strings[j];
if (cs.length() == 0) {
return false;
}
for (int i = 0; i < cs.length(); i++) {//如果保护不规则的字符,表示错误
char c = cs.charAt(i);
if (!Character.isLetter(c) && !Character.isDigit(c)) {
return false;
}
}
}
return true;
}