java Day7 正则表达式|异常

1、正则表达式

就是由一些特定的字符组成,完成一个特定的规则
可以用来校验数据格式是否正确。

1.1 常用

在这里插入图片描述
在这里插入图片描述

//字符类 匹配单个字符
System.out.println("a".matches("[abc]"));//true
System.out.println("a".matches("[^abc]"));//false
System.out.println("ab".matches("[abc]"));//false
System.out.println("B".matches("[a-zA-Z]"));//true
System.out.println("2".matches("[a-zA-Z]"));//false
System.out.println("b".matches("[a-z&&[^bc]]"));//false
System.out.println("z".matches("[a-z&&[^bc]]"));//true
System.out.println("z".matches("[a-z&&[^bc]]"));//true
//预定义字符 单个字符 \d \s \S \w \W
System.out.println("崔".matches("."));//t
System.out.println("崔1".matches("."));//f
System.out.println("1".matches("\\d"));//[0-9] //t
System.out.println("a".matches("\\d")); //false
System.out.println(" ".matches("\\s"));  //一个空白字符 //true
System.out.println("a".matches("\\S")); //一个非空白字符 //true
System.out.println("_".matches("\\w")); //[a-zA-Z0-9] //true
System.out.println("崔".matches("\\W")); //[^a-zA-Z0-9] //truE
//数量词 ? * + {n} {n,} {n,m}              System.out.println("a12".matches("\\w{3}"));//true               System.out.println("a1".matches("\\w{3,}"));//false
System.out.println("a12345".matches("\\w{3,5}"));//false
//其他几个常用的 () 分组  |或者 (?i) 忽略大小写
System.out.println("abC".matches("(?i)abc"));//true
System.out.println("aBC".matches("a((?i)b)c"));//false
System.out.println("123".matches("\\d{3}|[a-z]{3}"));//true
System.out.println("我爱编程666666".matches("我爱(编程)*(666)+"));//true
System.out.println("我爱编程6666666".matches("我爱(编程)*(666)+"));//false

1.2 字符串匹配,提取,分割

 //用于字符串匹配
            System.out.println(checkphone("16603809725"));
System.out.println(checkphone("010-12456789"));
//查找某段字符串中符合要求的 字符
String s="电话:166038069725 \n"+",010-123456789"+"姓名:崔凯悦";
String regex="(1[3-9]\\d{9})|(0\\d{2,7}-?[1-9]\\d{4,11})";
Pattern compile = Pattern.compile(regex);
Matcher matcher = compile.matcher(s);
while(matcher.find()){
 System.out.println(matcher.group());
//16603806972
//010-123456789
}
//替换,分割
String s1="翠翠翠翠asd张吱吱吱吱bgc急急急";
String s2 = s1.replaceAll("\\w+", "-"); //翠翠翠翠-张吱吱吱吱-急急急
System.out.println(s2);
String s3="我喜喜喜喜欢欢编程";
System.out.println(s3.replaceAll("(.)\\1+", "$1")); //我喜欢编程
String s4="崔凯悦123出处456姜龙翔abc";
String[] names = s4.split("\\w+");
System.out.println(Arrays.toString(names)); //[崔凯悦, 出处, 姜龙翔]

2、异常

异常就是程序出现的错误
在这里插入图片描述

2.1 运行时异常

就是只有我们在运行时才可能会发现的错误,在编译时不会提醒你。
比如:

        Integer.valueOf("abc");//运行时异常
//        Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
//        at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
//        at java.base/java.lang.Integer.parseInt(Integer.java:668)
//        at java.base/java.lang.Integer.valueOf(Integer.java:999)
//        at com.cky.mathclass.main.main(main.java:5)

2.2 编译时异常

在我们写程序时就会提醒我们需要捕获该异常 或者抛出该异常

 //②抛出
    public static void main(String[] args) throws ParseException {
//        Integer.valueOf("abc");//编译时异常
//        Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"
//        at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
//        at java.base/java.lang.Integer.parseInt(Integer.java:668)
//        at java.base/java.lang.Integer.valueOf(Integer.java:999)
//        at com.cky.mathclass.main.main(main.java:5)
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = null;//编译时异常  我们必须对其异常进行捕获或者声明抛出才可以
        //① 对其进行捕获
        try {
            parse = simpleDateFormat.parse("2000-6-11 12:15:13");
        } catch (ParseException e) {
            e.printStackTrace();
        }

        System.out.println(parse);
    }

2.3 自定义异常

在我们日常开发中,很多异常情况是java没有的,我们可以自己写一个异常类,注意 该类需要继承自运行时异常(runtimeException)或者编译时异常(exception)

至于到底继承哪个,要看自己,如果你觉得这个问题很严重,需要在编译时就告诉程序员,需要其进行捕获或者抛出,就继承编译时异常,否则继承运行时异常。

比如,我们需要保存一个合法的年龄。

2.3.1 自定义编译时异常

package com.cky.mathclass;
//编译时异常
public class AgeIllegailtyException1 extends  Exception{
    public AgeIllegailtyException1() {
    }

    public AgeIllegailtyException1(String message) {
        super(message);
    }
}

package com.cky.mathclass;

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

public class main {
    //②抛出
    public static void main(String[] args) throws ParseException {
        try {
            SavaAge1(100);
        } catch (AgeIllegailtyException1 e) {
            e.printStackTrace();
        }
    }

    public static void SavaAge1 (int age) throws AgeIllegailtyException1{
        if (age>0&&age<150){
            System.out.println("年龄保存成功");
        }
        else{
            //throw 跑出去这个异常对象上边调用者才会接受到这个异常,否则不会报错
            //throws 抛出方法内部的异常 用在方法上
            throw new AgeIllegailtyException1("/age is illegality,age is"+age);
        }
    }
}

2.3.2 自定义运行时异常

package com.cky.mathclass;

public class AgeIllegalityException extends RuntimeException{
    public AgeIllegalityException() {
    }

    public AgeIllegalityException(String message) {
        super(message);
    }
}

package com.cky.mathclass;

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

public class main {
    //②抛出
    public static void main(String[] args) throws ParseException {
     SavaAge(150);

    }
    public static void SavaAge(int age){
        if (age>0&&age<150){
            System.out.println("年龄保存成功");
        }
        else{
            //throw 跑出去这个异常对象上边调用者才会接受到这个异常,否则不会报错
            throw  new AgeIllegalityException("/age is illegality,age is"+age);
        }


    }
}

在这里插入图片描述

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值