邮箱验证信息

第一种是普通常规的验证方法:

1         //邮箱的验证,验证内容:1、是否有@符号,不能在开始位置,2、是否有.符号,不能在最后位置;3、@符号是否在.号前面
2         System.out.println("请输入邮箱:");
3         String email=input.nextLine();
4         //普通的验证合法性
5         if(email.indexOf("@")<0 || email.startsWith("@") || email.indexOf(".")<0 || email.endsWith(".") || email.indexOf("@")>email.lastIndexOf(".")){
6             System.out.println("不合法");
7         }else{
8             System.out.println("合法");
9     }

第二种是用正则表达式表示的:

使用正则表达式的方式验证合法性
	 Pattern pattern = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
	  Matcher match=pattern.matcher(email);
		if(match.matches()){
			System.out.println("合法");
		}else{
			System.out.println("不合法");
		}
		
		for(int i=0;i<str.length();i++){
			System.out.println(str.charAt(i));
		}
原文地址:https://www.cnblogs.com/zhuangjixiang/p/2791341.html