JAVA | 37 - 正则表达式 | String 类对正则的支持

利用正则实现验证,代码可以最少化。

替换

public class Main {
    public static void main(String[] args) {
        String str = "fghkhjgkfjk648743fgd8gfg34374?,<$%^&*87";
        String regex = "[^a-z]";
        System.out.println(str.replaceAll(regex,""));
    }
}

拆分

public class Main {
    public static void main(String[] args) {
        String str = "fghkd67hjgkfjk648743fgd833gfg34374?,<$%4343^&*87";
        String regex = "\\d+";
        String array [] = str.split(regex);
        for(int i = 0; i < array.length; i ++){
            System.out.println(array[i]);
        }
    }
}

判断

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    public static void main(String[] args) {
        String str = "12.678";
        String regex = "\\d+(\\.\\d+)?"; // 判断整数或者小数
        System.out.println("digital:" + str.matches(regex));
        if(str.matches(regex)){
            System.out.println(Double.parseDouble(str));
        }

        String str2 = "192.168.1.103";
        String regex2 = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"; // 判断 ip 地址
        System.out.println("ip:" + str2.matches(regex2));

        String str3 = "2017-11-30";
        String regex3 = "\\d{4}-\\d{2}-\\d{2}"; // 判断日期
        Date date =new Date();
        if(str3.matches(regex3)){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try{
                date = simpleDateFormat.parse(str3);
            }catch (Exception e){
                e.printStackTrace();
            }
            System.out.println(date);
        }

        String str4 = "(0916)-4356110";
        String str5 = "0916-4356110";
        String str6 = "4356110";
        String regex4 = "((\\(\\d{4}\\)-)|(\\d{4}-))?\\d{7}"; // 判断电话号码
        System.out.println("number:" + str4.matches(regex4));
        System.out.println("number:" + str5.matches(regex4));
        System.out.println("number:" + str6.matches(regex4));

        String str7 = "13122111623@sina.cn";
        String str8 = "13122111623@sina.com";
        String str9 = "972633408@qq.com";
        String str10 = "yuzhen233@hotmail.com";
        String str11 = "yuzhen233@gmail.com";
        String str12 = "151340218@mail.dhu.edu.cn";
        String str13 = "151340218@dhu.edu.cn";
        String regex5 = "(\\d{11}@sina\\.(cn|com))" +
                "|((\\d{5}|\\d{9}|\\d{10})@qq.com)" +
                "|([a-zA-z]+\\d+@(hotmail|gmail).com)" +
                "|(\\d{9}@((mail.dhu.edu.cn)|(dhu.edu.cn)))"; // 判断邮件地址
        System.out.println("mailAdress:"+ str7.matches(regex5));
        System.out.println("mailAdress:"+ str8.matches(regex5));
        System.out.println("mailAdress:"+ str9.matches(regex5));
        System.out.println("mailAdress:"+ str10.matches(regex5));
        System.out.println("mailAdress:"+ str11.matches(regex5));
        System.out.println("mailAdress:"+ str12.matches(regex5));
        System.out.println("mailAdress:"+ str13.matches(regex5));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值