“正则表达式 练习:验证邮箱地址”的版本间的差异
		
		
		
		
		
		跳到导航
		跳到搜索
		
				
		
		
	
Jihongchang(讨论 | 贡献)  | 
				Jihongchang(讨论 | 贡献)   | 
				||
| 第9行: | 第9行: | ||
写出对应的正则表达式,验证输入的字符串是否满足规则  | 写出对应的正则表达式,验证输入的字符串是否满足规则  | ||
| + | |||
| + | ==== 自己实现的 ====  | ||
| + | <syntaxhighlight lang="java">  | ||
| + | public class Homework01 {  | ||
| + | |||
| + |     public static void main(String[] args) {  | ||
| + | |||
| + |         String regex = "[a-zA-Z0-9_\\-]+@((\\w+\\.))+\\w+";  | ||
| + | |||
| + |         Homework01 work = new Homework01();  | ||
| + | |||
| + |         System.out.println("work.test1(regex) success:" + work.test1(regex));  | ||
| + | |||
| + |         System.out.println("work.test2(regex) success:" + work.test2(regex));  | ||
| + | |||
| + |         System.out.println("work.test3(regex) success:" + work.test3(regex));  | ||
| + | |||
| + |         System.out.println("work.test4(regex) success:" + work.test4(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);  | ||
| + | |||
| + |     }  | ||
| + | |||
| + | }  | ||
| + | </syntaxhighlight><syntaxhighlight lang="console">  | ||
| + | work.test1(regex) success:true  | ||
| + | work.test2(regex) success:true  | ||
| + | work.test3(regex) success:true  | ||
| + | work.test4(regex) success:true  | ||
| + | </syntaxhighlight>  | ||
2022年11月20日 (日) 17:13的版本
https://www.bilibili.com/video/BV1Eq4y1E79W?p=24
验证电子邮件格式是否合法
规定电子邮件规则为:
- 只能有一个@
 - @前面是用户名,可以是 a-z、A-Z、0-9、 _(下划线)、-(减号、中划线)字符
 - @后面是域名,并且域名只能是英文字母,比如 sohu.com 或者 tsinghua.org.cn
 
写出对应的正则表达式,验证输入的字符串是否满足规则
自己实现的
public class Homework01 {
    public static void main(String[] args) {
        String regex = "[a-zA-Z0-9_\\-]+@((\\w+\\.))+\\w+";
        Homework01 work = new Homework01();
        System.out.println("work.test1(regex) success:" + work.test1(regex));
        System.out.println("work.test2(regex) success:" + work.test2(regex));
        System.out.println("work.test3(regex) success:" + work.test3(regex));
        System.out.println("work.test4(regex) success:" + work.test4(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);
    }
}
work.test1(regex) success:true
work.test2(regex) success:true
work.test3(regex) success:true
work.test4(regex) success:true