题目:定义判断电子邮箱的正则表达式,判断输入的字符串是否为电子邮箱地址。
代码:
package My; import java.util.*; public class Test11_23 { public static void main(String[] args) { // TODO Auto-generated method stub String regex = "\w+[@]\w+[.][comn]+[.]*[comn]*"; System.out.println("请输入邮箱"); Scanner reader = new Scanner(System.in); String s1 = reader.next(); if(s1.matches(regex)) { System.out.println("邮箱合法"); } else { System.out.println("邮箱不合法"); } } }
运行结果: