1.记事本的打开与关闭
注:Runtime获取当前虚拟机信息
2.年月日 时分秒
3.判断电话
4.邮箱
import java.util.Scanner; public class EmailCheck { public static boolean checkEmail(String email) { String regex1 = "[a-zA-Z]+[a-zA-Z0-9_]*@[a-zA-Z0-9]+[.][a-zA-Z0-9]+"; //字母开头,@后加字母或数字,后面加点,后面字母或数字 String regex2 = "[a-zA-Z]+[a-zA-Z0-9_]*@[a-zA-Z0-9]+[.][a-zA-Z0-9]+[.][a-zA-Z0-9]+"; //..........在regex1基础上,后面加.和其他字母组成的后缀 if(email.matches(regex1) || email.matches(regex2)) { System.out.println("合法邮箱地址"); return true; } else { System.out.println("不合法邮箱地址"); return false; } } public static boolean checkabc(String email) { //判断是否包含 某字符串,判断是某个网站的邮箱 if(email.indexOf("@abc.com") > -1) { System.out.println("abc"); return true; } else { System.out.println("非abc"); return false; } } } class Entry { public static void main(String[] args) { Scanner input = new Scanner(System.in); String email = input.next(); EmailCheck.checkEmail(email); EmailCheck.checkabc(email); } }
5.用户注册信息进行限制
import java.util.Scanner; public class Demo1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请填写用户名:"); String name = sc.next(); //必须是6-10位字母、数字、下划线 不能以数字开头 while(!checkName(name)) { System.out.println("用户名不合法,请重新输入:"); name=sc.next(); } //必须是6-20位的字母、数字、下划线 System.out.println("请填写密码:"); String pwd = sc.next(); while(!checkPwd(pwd)) { System.out.println("密码不合法,请重新输入:"); pwd=sc.next(); } System.out.println("注册成功!"); } /** * 用户名验证 * @param name * @return */ public static boolean checkName(String name) { String regExp = "^[^0-9][\\w_]{5,9}$"; if(name.matches(regExp)) { return true; }else { return false; } } /** * 密码验证 * @param pwd * @return */ public static boolean checkPwd(String pwd) { String regExp = "^[\\w_]{6,20}$"; if(pwd.matches(regExp)) { return true; } return false; } }
技能点:利用正则表达式,对用户注册信息进行限制