首页 > 解决方案 > 如何验证电子邮件(仅包含数字、字母、星号、at、下划线、点)

问题描述

我需要验证电子邮件以进行练习。电子邮件只允许包含:

如果电子邮件不包含@但以星号结尾,则应扩展一个字符串以使其成为电子邮件。我尝试使用 ASCII 码,但这只是很多。我还看到正则表达式是一回事,但我无法理解它。

标签: java

解决方案


我认为使用 Regex 是一个方便的解决方案。这很容易实现。您可以在 google 搜索中找到一些正则表达式电子邮件验证器模式。

这是验证电子邮件的示例正则表达式模式:^(.+)@(.+)$

    //1st way  
Pattern p = Pattern.compile("^(.+)@(.+)$");//. represents single character  
Matcher m = p.matcher("sapmle@sample.com");  
boolean b = m.matches();  // returns true
  
//2nd way  
boolean b2=Pattern.compile("^(.+)@(.+)$").matcher("[text you want to validate]").matches();  
  
//3rd way  
boolean b3 = Pattern.matches("^(.+)@(.+)$", "[text you want to validate]");  

推荐阅读