Java正则表达式

 

  • 正则表达式初体验:

需求:假如现在要求校验一个QQ号码是否正确,6~20位之内的全数字组合而成。

 

 1 public static void main(String[] args) {
 2         //需求:校验QQ号码,必须全部为数字 6 ~20 位
 3         System.out.println(checkQQ("456789afv5"));
 4         System.out.println(checkQQ("4567895"));
 5         System.out.println(checkQQ(null));
 6         System.out.println("-----------");
 7         System.out.println(regexCheckQQ("456789afv5"));
 8         System.out.println(regexCheckQQ("4567895"));
 9         System.out.println(regexCheckQQ(null));
10     }
11     //普通校验方法
12     public static boolean checkQQ(String qq){
13         if(qq == null || qq.length() < 6 || qq.length() > 20){
14             return false;
15         }
16         else {
17             //遍历每一个字符
18             for (int i = 0; i < qq.length(); i++) {
19                 if(qq.charAt(i) < '0' ||  qq.charAt(i) > '9'){
20                     return false;
21                 }
22             }
23             return true;
24         }
25     }
26     //使用正则表达式
27     public static boolean regexCheckQQ(String qq){
28         return qq != null && qq.matches("\\d{6,20}");
29     }

 

示例结果:

 

 

 可以看到,正则表达式的功能还是挺方便的,功能也很强大,一行代码就能搞定任务。

正则表达式的匹配规则(其中,API文档内列出了全部的匹配格式):

 

 

 对这些较为常用的API做了一个简单的总结

 

 1 public static void main(String[] args) {
 2         //1、必须是数字、字母、下划线,至少6位
 3         System.out.println("25987fe56_789".matches("\\w{6,}"));
 4         System.out.println("e_789".matches("\\w{6,}"));
 5         //2、必须是数字和字符,必须是4位
 6         System.out.println("456cbe".matches("[a-zA-Z0-9]{4}"));
 7         System.out.println("456_cbe".matches("[a-zA-Z0-9]{4}"));
 8         System.out.println("4cbe".matches("[a-zA-Z0-9]{4}"));
 9         System.out.println("-------------------------");
10         //当然,以上匹配规则也可以是 \w(英文字母、数字、下划线) 和 非下划线(^_)求交集
11         System.out.println("456cbe".matches("[\\w && [^_]]{4}"));
12         System.out.println("456_cbe".matches("[\\w && [^_]]{4}"));
13         System.out.println("4cbe".matches("[\\w && [^_]]{4}"));
14     }

 

 

  •  正则表达式案例:
 1  public static void main(String[] args) {
 2         Scanner sc = new Scanner(System.in);
 3         //手机号
 4         checkPhoneNumber(sc);
 5         //邮箱号
 6         checkEmail(sc);
 7         //座机号
 8         checkFixPhoneNumber(sc);
 9         //金额
10         checkMoney(sc);
11     }
12 
13     private static void checkMoney(Scanner sc) {
14         while (true){
15             System.out.println("请输入金额:");
16             String inputMoney = sc.next();
17             if(inputMoney.matches("\\d{1,}(\\.\\d{1,2}){0,1}")){
18                 System.out.println("金额匹配成功");
19                 break;
20             }
21             else {
22                 System.out.println("金额匹配失败");
23             }
24         }
25     }
26 
27     /**
28      * 010-36598789
29      * 060334687
30      * @param sc
31      */
32     private static void checkFixPhoneNumber(Scanner sc) {
33         while (true){
34             System.out.println("请输入座机号:");
35             String fixPhoneNumber = sc.next();
36             if(fixPhoneNumber.matches("0[0-9]{2}[-]?[0-9]{5,10}")){
37                 System.out.println("座机号匹配成功");
38                 break;
39             }
40             else {
41                 System.out.println("座机号匹配失败!");
42             }
43         }
44     }
45 
46     public static void checkPhoneNumber(Scanner sc){
47         while (true) {
48             System.out.println("请输入手机号:");
49             String phoneNumber = sc.next();
50             if(phoneNumber.matches("1[3456789]\\d{9}")){
51                 System.out.println("手机号格式正确");
52                 break;
53             }
54             else {
55                 System.out.println("手机号格式不正确!");
56             }
57         }
58     }
59 
60     /**
61      * 邮箱格式:32568945@qq.com、
62      * 4568924@163.com、
63      * 两级域名:32498657@pci.com.cn
64      * @param sc
65      */
66     public static void checkEmail(Scanner sc){
67         while (true) {
68             System.out.println("请输入邮箱:");
69             String emailNumber = sc.next();
70             if(emailNumber.matches("\\w{6,20}@[\\w && [^_]]{2,3}(\\.[\\w && [^_]]{2,3}){1,2}")){
71                 System.out.println("邮箱格式正确");
72                 break;
73             }
74             else {
75                 System.out.println("邮箱格式不正确!");
76             }
77         }
78     }

运行示例:                                          

          

 

 正则表达式在字符串方法中的应用:

 

  •  拓展:正则表达式爬取信息
 1 public static void main(String[] args) {
 2         String str = "欢迎来到XXXX,电话号码,你可以通过以下方式联系我们:电话1:010-23598745652,或者" +
 3                 "联系邮箱:897456@com.cn;电话2:40025216359";
 4         //需求:从以上内容中爬取电话号码和邮箱
 5         //1、定义爬取规则:电话1、邮箱、电话2
 6         String regex = "(\\w{6,20}@[\\w && [^_]]{2,3}(\\.[\\w && [^_]]{2,3}){1,2})|" +
 7                 "(0[0-9]{2}[-]?[0-9]{5,10})|" +
 8                 "(400-?\\d{6,15})";
 9         //2、把爬取规则编译成匹配对象
10         Pattern pattern = Pattern.compile(regex);
11         //3、创建一个内容匹配对象
12         Matcher matcher = pattern.matcher(str);
13         //4、开始匹配
14         while (matcher.find()){
15             String ret = matcher.group();
16             System.out.println(ret);
17         }
18     }

示例运行结果:

 

posted @ 2022-05-06 15:41  羽梦齐飞  阅读(44)  评论(0编辑  收藏  举报