第13次作业--邮箱的正则表达式
题目:定义判断电子邮箱的正则表达式,判断输入的字符串是否为电子邮箱地址。
1.代码部分
package com; import java.util.Scanner; public class test { /** * 题目:定义判断电子邮箱的正则表达式, * 判断输入的字符串是否为电子邮箱地址。 */ public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("请输入邮箱地址"); String str=input.nextLine(); String regex="[a-zA-Z0-9_-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.(cn|com)"; if(str.matches(regex)){ System.out.println(str+"是一个正确的邮箱地址"); } else{ System.out.println(str+"不是一个正确的邮箱地址"); } } }
2.测试运行