第13次作业--邮箱的正则表达式
题目:定义判断电子邮箱的正则表达式,判断输入的字符串是否为电子邮箱地址。
1 package 邮箱合法; 2 3 import java.util.Scanner; 4 5 public class Test { 6 7 8 public static void main(String[] args) { 9 Scanner reader=new Scanner (System.in); 10 System.out.println("请输入邮箱:"); 11 String s=reader.nextLine(); 12 String regex="\\w+@\\w+.[a-zA-Z0-9-]{2,}"; 13 if(s.matches(regex)){ 14 System.out.println(s+"是一个合法的邮箱"); 15 }else{ 16 System.out.println(s+"不是一个合法的邮箱"); 17 } 18 19 } 20 21 }
运行结果: