木舟0基础学习Java的第十二天(正则,常用工具类)

正则表达式(regex)

概述:指一个用来描述或者匹配一系列符合语法规则的字符串的单个字符串(其实就是个规则)

作用:比如注册用户名和密码,电话号码,身份证号码,邮箱,网址... 对其进行约束长度 那这个工作就是由正则完成的

用法:  匹配 matches

String regex="[1-9]\\d{4,11}";
System.out.println("494408337".matches(regex));

常见对象:预定义字符类 (匹配的都是单个字符!)

.  任意字符 一个点代表一个任意字符

\d  数字 等同于[0-9]

\w  单词字符 等同于[a-z A-Z_0-9]

[abc]  a或者b或者c

[^abc]  任意字符除了abc 

[a-z A-Z]  a-z A-Z 两头的字母包括在内

[0-9] 0-9的字符都包括

 System.out.println("匹配任意一个字符");
        String regex1=".";
        System.out.println("我".matches(regex1));//true
        System.out.println("我们".matches(regex1));//false

        System.out.println("匹配\\d匹配0-9之间任意一个字符");
        String regex2="\\d";
        System.out.println("0".matches(regex2));//true
        System.out.println("01".matches(regex2));//false

        System.out.println("匹配\\w匹配a-zA-Z_0-9之间任意一个字符");
        String regex3="\\w";
        System.out.println("G".matches(regex3));//true
        System.out.println("b".matches(regex3));//true
        System.out.println("9".matches(regex3));//true

        System.out.println("匹配[abc]匹配a或者b或者c");
        String regex4="[abc]";
        System.out.println("b".matches(regex4));//true
        System.out.println("d".matches(regex4));//false

        System.out.println("匹配[^abc]匹配除了abc以外的任意字符");
        String regex5="[^abc]";
        System.out.println("a".matches(regex5));//false
        System.out.println("6".matches(regex5));//true

        System.out.println("匹配[a-zA-Z]匹配a-zA-Z之间的字符包括头尾");
        String regex6="[a-zA-Z]";
        System.out.println("a".matches(regex6));//true
        System.out.println("Z".matches(regex6));//true

        System.out.println("匹配[0-9]匹配0-9的字符");
        String regex7="[0-9]";
        System.out.println("9".matches(regex7));//true

常见对象:数量词

X?  X出现1次或者0次

X*  X出现0次或者多次

X+  X出现1次或者多次

X{n} X出现恰好n次

X{n,}  X出现至少n次

X{n,m}  X出现至少n次 但是不超过m次

System.out.println("----------?----------");
        // a或者b或者c中任意一个出现1次或者0次
        String regex1="[abc]?";
        System.out.println("a".matches(regex1));//true
        System.out.println("d".matches(regex1));//false

        System.out.println("----------*----------");
        //除了abc其他的出现0次或者多次
        String regex2="[^abc]*";
        System.out.println("def123+-=你好".matches(regex2));//true
        System.out.println("c".matches(regex2));//false

        System.out.println("----------+----------");
        //0-9中任意一个数至少出现1次
        String regex3="[0-9]+";
        System.out.println("0012346789".matches(regex3));//true
        System.out.println("0".matches(regex3));//true

        System.out.println("----------{n}----------");
        //a-zA-Z中任意字符恰好出现4次
        String regex4="[a-zA-Z]{4}";
        System.out.println("azAZ".matches(regex4));//true
        System.out.println("abC".matches(regex4));//false
        System.out.println("abC1".matches(regex4));//false

        System.out.println("----------{n,}----------");
        //nihao当中任意字符最少出现两次最多无限次
        String regex5="[nihao]{2,}";
        System.out.println("nihao".matches(regex5));//true
        System.out.println("n".matches(regex5));//false
        System.out.println("nnnniiiihhhhaaaaoooo".matches(regex5));//true

        System.out.println("----------{n,m}----------");
        //当中任意字符最少出现3次最多出现4次
        String regex6="[abc]{3,4}";
        System.out.println("a".matches(regex6));//false
        System.out.println("abcc".matches(regex6));//true
        System.out.println("abccc".matches(regex6));//false

案例:

public class Demo01 {
    public static void main(String[] args){
        //案例:校验QQ号码
        //必须5-12位数字 0不能开头 必须都是数字
        System.out.println(checkQQ("494408337"));
        //用正则表达式做
        // [1-9]表示 匹配1-9之间任意一个数字
        // \\d 第一个\是转译符 \d是表示0-9之间的数字
        // {4,11} 表示\\d最多重复11次 最少重复出现4次
        String regex="[1-9]\\d{4,11}";
        //匹配 matches
        System.out.println("494408337".matches(regex));
    }
    //没用正则表达式做
    public static boolean checkQQ(String qq){
        boolean flag=true;//如果校验qq不符合要求就设置flag为false
        if(qq.length()>=5&&qq.length()<=12){
            //判断 qq是否以0开头
            if(!qq.startsWith("0")){
                //将字符串转换为字符数组
                char[] c=qq.toCharArray();
                for(int i=0;i<c.length;i++){
                    //记录每一个字符
                    char ch=c[i];
                    if(!(ch>='0' && ch<='9')){
                        flag=false;//不是数字
                        break;
                    }else{
                        flag=true;//是数字
                    }
                }
            }else{
                flag=false;
            }
        }else{
            flag=false;
        }
        return flag;
    }
}

正则分割:split(),替换所有 replaceAll()

   //正则分割 split()
        String s="我N爱o学1习";
        //要求输出我爱学习
        //方法1
        String[] arr=s.split("\\w");
        for(int i=0;i<arr.length;i++){
            System.out.print(arr[i]);//我爱学习
        }

        //方法2 替换所有 replaceAll 将\\w的字符替换成""空
        String ss=s.replaceAll("\\w","");
        System.out.println(ss);//我爱学习

案例:

  //给定字符串中的数字排序
        // 91 27 38 46 50 要求输出 27 38 46 50 91
        String s=" 91 27 38 46 50 ";
        String ss=s.trim();//去掉两头的空格
        String[] arr=ss.split(" ");//以空格进行分割
        Arrays.sort(arr);//排序
        //不换行遍历
        for(String str:arr){
            System.out.print(str+" ");
        }

正则叠词切割:

  //匹配叠词的正则表达式 用分组
        String s1="快快乐乐";
        String regex1="(.)\\1(.)\\2";
        System.out.println(s1.matches(regex1));//true
        String s2="开心开心";
        String regex2="(..)\\1";
        System.out.println(s2.matches(regex2));//true
        String s3="幸福啊幸福啊";
        String regex3="(...)\\1";
        System.out.println(s3.matches(regex3));//true
    }

案例:/"$1"代表叠词中单个字符的本身

public static void main(String[] args) {
        String s="我我....我...我.要...要要...要学....学学..学.编..编编.编.程.程.程..程";
        String ss=s.replaceAll("\\.","");
        System.out.println(ss);
        String regex="(.)\\1+";
        //"$1"代表叠词中单个字符的本身
        String sss=ss.replaceAll(regex,"$1");
        System.out.println(sss);//我要学编程
    }

Pattern类和Matcher

public static void main(String[] args) {
      /*  Pattern p= Pattern.compile("a*b");//获取到正则表达式
        Matcher m=p.matcher("aaaaab");
        boolean b=m.matches();
        System.out.println(b);//true*/


        Pattern p=Pattern.compile("[abc]+");
        String s="bbb";
        Matcher m=p.matcher(s);
        boolean b=m.matches();
        System.out.println(b);//true

        //上面这段代码就相当于 s.matcher("[abc]+");
    }

案例:手机号都是乱编的 (如果真有人在用 通知我我会立刻删)

  public static void main(String[] args) {
        String s="客户登记的手机号码为13547583392曾用手机号18547389921,第三个手机号15598765432";
        String regex="1[3-9]\\d{9}";//手机号的正则表达
        Pattern p=Pattern.compile(regex);
        Matcher m=p.matcher(s);
        while(m.find()){
            System.out.println(m.group());
        }
    }

Java中的常用工具类

System

    public static void main(String[] args) {
        //获取当前时间的毫秒值
        long time=System.currentTimeMillis();
        System.out.println(time);//1720166245748
        //正常退出
        System.exit(0);
        //不正常退出
        System.exit(-1);
        //通知垃圾回收机制可以回收垃圾了(一般不会用)
        System.gc();
    }

BigInterger(针对int和long)

 public static void main(String[] args) {
        //long l=123125134124313213123213123l;//超长了
        BigInteger b=new BigInteger("123125134124313213123213123");
        BigInteger b1=new BigInteger("123456789");

        //加法计算 (b+b1)
        BigInteger result=b.add(b1);
        System.out.println(result);//123125134124313213246669912

        //减法计算
        BigInteger result1=b.subtract(b1);
        System.out.println(result1);//123125134124313212999756334

        //除法计算
        BigInteger result2=b.divide(b1);
        System.out.println(result2);//997313595482490745

        //除法后带小数部分
        BigInteger[] result3=b.divideAndRemainder(b1);
        //打印小数长度
        System.out.println(result3.length);
        System.out.println("小数点之前"+result3[0]+"小数点之后"+result3[1]);//小数点之前997313595482490745小数点之后23295318
    }

BigDecimal(针对float和double)

public static void main(String[] args) {
        System.out.println(2.0-1.1);//Java中浮点数在运算时会存在误差 结果无限接近0.9
        //减法
        BigDecimal b=new BigDecimal("2.0");
        BigDecimal b1=new BigDecimal("1.1");
        BigDecimal result=b.subtract(b1);
        System.out.println(result);//0.9
        //加减乘与 BigInteger 用法一样 但是除法除不尽会抛异常
        //除法
        BigDecimal b3=new BigDecimal("10000000000000");
        BigDecimal b4=new BigDecimal("3");
        BigDecimal result1=b3.divide(b4);
        System.out.println(result1);//ArithmeticException 算数异常
        // 当出现异常的运算条件时,抛出此异常。例如,一个整数“除以零”时,抛出此类的一个实例。
    }

Date(时间)

import java.util.Date;
//import java.sql.Date;同名的类会冲突 所以一般使用全路径名


public class Date01 {
    public static void main(String[] args) {
        Date d1=new Date();
        System.out.println(d1);//Fri Jul 05 16:52:35 CST 2024
        long time=System.currentTimeMillis();//毫秒
        Date d2=new Date(time);
        System.out.println(d2);//Fri Jul 05 16:56:25 CST 2024
        System.out.println(d2.after(d1));//true
        System.out.println(d2.before(d1));//false
        System.out.println(d2.getDate());//5
        System.out.println(d2.getTime());//1720169880849

        //只有日期 数据库用
        //全路径名称 java.sql.Date
        java.sql.Date dd=new java.sql.Date(System.currentTimeMillis());
        System.out.println(dd);//2024-07-05
    }
}
  public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat();
        System.out.println(sdf);//java.text.SimpleDateFormat@b5341f2a
        Date d=new Date();
        System.out.println(d);//Fri Jul 05 17:07:04 CST 2024
        String s=sdf.format(d);//转换格式
        System.out.println(s);//24-7-5 下午5:07

        //指定格式转换
        SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//年-月-日 时:分:秒
        String sdate=sdf2.format(new Date());//匿名对象
        System.out.println(sdate);//2024-07-05 17:10:48

        //将上面的代码反向转换
        Date d2=sdf2.parse("2024-07-05 17:10:48");//parse会报异常
        System.out.println(d2);//Fri Jul 05 17:10:48 CST 2024
    }

案例:从出生到现在活了多少天

public static void main(String[] args) throws ParseException {
        //算一下你来到这个世界多少天
        String sDate="2000-1-05";
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date d=sdf.parse(sDate);
        long time=d.getTime();//获取生日当天 到1970年1月1日0时0分0秒 的毫秒值
        System.out.println(time);//947001600000
        //获取当前毫秒值
        long time1=System.currentTimeMillis();
        System.out.println(time1);//1720171333778
        long bir=time1-time;
        int day=(int)(bir/1000/3600/24);//算多少天
        System.out.println("从出生到现在活了"+day+"天");
    }

Calendar:

 public static void main(String[] args) {
        //Calendar 抽象类
        Calendar c=Calendar.getInstance();//多态 父类指向子类
        System.out.println(c);

        //输出当前单独的日
        int date=c.get(Calendar.DATE);
        System.out.println(date);//5

        //输出当前单独月
        //6要加1 在格里高利万年历中月份是0-11之间 0代表1月
        int month=c.get(Calendar.MONTH);
        System.out.println(month);//6

        //往当前的日加5天
        int day=c.get(Calendar.DAY_OF_MONTH);
        System.out.println("修改前"+day);
        c.add(Calendar.DAY_OF_MONTH,5);
        System.out.println("修改后"+c.get(Calendar.DAY_OF_MONTH));

        //算香港回归是星期几
        Calendar c1=Calendar.getInstance();
        c1.set(1997,6,1);
        //星期是0-6 0代表周一
        int week=c1.get(Calendar.DAY_OF_WEEK);
        System.out.println(week);//3
    }

自动拆箱和自动装箱:包装类

byte

Byte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

  public static void main(String[] args) {
        //自动装箱 将int类型的10 转换为字符串
        Integer i1=new Integer(10);
        Integer i2=new Integer("10");
        System.out.println(i1+"---"+i2);//10---10
        Integer i3=10;//底层将基本数据类10包装成了Integer对象
        Double d=54.6;//底层将基本数据类double 包装成了Double对象
        System.out.println(d+3);//57.6  对象的类型被自动拆装成了基本数据类型
        //默认调用了以下方法
        System.out.println(d.doubleValue()+3);//57.6

        //自动拆装
        int num=i3;//把对象赋值给一个基本数据类型
        System.out.println(num*10);//100

        //自动包装
        Float f=34.5f;
        Boolean b=true;
        Character c='蒙';

        //自动拆装
        char c1=c;
        System.out.println(c1);//蒙

    }

字符串类型转基本数据类型:

  //字符串类型转基本数据类型
        //parse***(String s)
        //Integer.toBinaryString(int i)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值