java - 如何验证电子邮件(仅包含数字、字母、星号、at、下划线、点)
问题描述
我需要验证电子邮件以进行练习。电子邮件只允许包含:
- 0-9
- 阿兹
- AZ
- @
- 星号
- 下划线
- 观点
如果电子邮件不包含@但以星号结尾,则应扩展一个字符串以使其成为电子邮件。我尝试使用 ASCII 码,但这只是很多。我还看到正则表达式是一回事,但我无法理解它。
解决方案
我认为使用 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]");
推荐阅读
- symfony5 - 功能测试 EasyAdmin 3 后端
- postgresql - AddClaimAsync 上的 EFCore 身份异常
- python - 获取网格节点的邻居
- python - Pipreqs 为 Hydra 生成不正确的需求
- entity-framework-core - 使用 keyvault 中的连接字符串运行 dotnet EF 迁移时出现 Azure devops 管道错误
- flutter - Flutter - 将 json 数据保存在本地以供将来使用
- ajax - AJAX xhr 进度功能和经典 asp response.clear/response.flush 不起作用
- github - 创建另一个 github 帐户并添加新 ssh 导致的 ssh 问题
- django - 如何使用中间件为 HTTP 请求添加属性
- reactjs - 使用 React 和 Express 上传图片