Java后台邮箱格式验证

 

代码:

 

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegisterCheck {
    /**
     * 验证输入的邮箱格式是否符合
     * @param email
     * @return 是否合法
     */
public static boolean emailFormat(String email)
    {
        boolean tag = true;
        final String pattern1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        final Pattern pattern = Pattern.compile(pattern1);
        final Matcher mat = pattern.matcher(email);
        if (!mat.find()) {
            tag = false;
        }
        return tag;
    }

public static void main(String args[]){
	System.out.println("请输入邮箱:");
	Scanner sc=new Scanner(System.in);
  	String email=sc.next();
  	if(RegisterCheck.emailFormat(email)){
  		System.out.println("√");
  		return;
  	}
  	else{
  		System.out.println("您的邮箱格式不正确,请重新输入!");
  		main(args);
  	  	}	
}
}

 

你可能感兴趣的:(java,验证,格式)