查看“正则表达式 练习:验证邮箱地址”的源代码
←
正则表达式 练习:验证邮箱地址
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
https://www.bilibili.com/video/BV1Eq4y1E79W?p=24 === 验证电子邮件格式是否合法 === 规定电子邮件规则为: # 只能有一个@ # @前面是用户名,可以是 a-z、A-Z、0-9、 _(下划线)、-(减号、中划线)字符 # @后面是域名,并且域名只能是英文字母,比如 sohu.com 或者 tsinghua.org.cn 写出对应的正则表达式,验证输入的字符串是否满足规则 ==== 自己实现的 ==== <syntaxhighlight lang="java"> public class Homework01 { public static void main(String[] args) { String regex = "[\\w-]+@([a-zA-Z]+\\.)+[a-zA-Z]+"; Homework01 work = new Homework01(); String content = "jihongchang@jihongchang.top"; System.out.printf("\"%s\".matches(regex):%b \n", content, content.matches(regex)); content = "jihongchang@"; System.out.printf("\"%s\".matches(regex):%b \n", content, content.matches(regex)); content = "jihongchang@jihongchang"; System.out.printf("\"%s\".matches(regex):%b \n", content, content.matches(regex)); content = "jihongchang@jihongchang."; System.out.printf("\"%s\".matches(regex):%b \n", content, content.matches(regex)); content = "ji_hong-chang@jihongchang.top"; System.out.printf("\"%s\".matches(regex):%b \n", content, content.matches(regex)); content = "ji_hong-chang@jihongchang.@jihongchang.top"; System.out.printf("\"%s\".matches(regex):%b \n", content, content.matches(regex)); } public boolean test1(String regex) { String content = "jihongchang@jihongchang.top"; return content.matches(regex); } public boolean test2(String regex) { String content = "jihongchang@"; return !content.matches(regex); } public boolean test3(String regex) { String content = "jihongchang@jihongchang"; return !content.matches(regex); } public boolean test4(String regex) { String content = "jihongchang@jihongchang."; return !content.matches(regex); } public boolean test5(String regex) { String content = "ji_hong-chang@jihongchang.top"; return content.matches(regex); } public boolean test6(String regex) { String content = "ji_hong-chang@jihongchang.@jihongchang.top"; return content.matches(regex); } } </syntaxhighlight><syntaxhighlight lang="console"> "jihongchang@jihongchang.top".matches(regex):true "jihongchang@".matches(regex):false "jihongchang@jihongchang".matches(regex):false "jihongchang@jihongchang.".matches(regex):false "ji_hong-chang@jihongchang.top".matches(regex):true "ji_hong-chang@jihongchang.@jihongchang.top".matches(regex):false </syntaxhighlight>注意:String matches 是整体匹配
返回至
正则表达式 练习:验证邮箱地址
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息