一、题目
定义判断电子邮箱的正则表达式,判断输入的字符串是否为电子邮箱地址。
二、源代码
package com1121;
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("请输入要验证的电子邮箱:");
String str=sc.nextLine();
//正则表达式
String regex="[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}";
if(str.matches(regex)){
System.out.println(str+"合法");
}else {
System.out.println(str+"错误");
}
}
}