java Day7 正则表达式|异常

news/2024/4/29 7:55:50

文章目录

  • 1、正则表达式
    • 1.1 常用
    • 1.2 字符串匹配,提取,分割
  • 2、异常
    • 2.1 运行时异常
    • 2.2 编译时异常
    • 2.3 自定义异常
      • 2.3.1 自定义编译时异常
      • 2.3.2 自定义运行时异常

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);}}
}

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.cpky.cn/p/10591.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

DataFunSummit 2023因果推断在线峰会:解码数据与因果,引领智能决策新篇章(附大会核心PPT下载)

在数据驱动的时代&#xff0c;因果推断作为数据科学领域的重要分支&#xff0c;正日益受到业界的广泛关注。DataFunSummit 2023年因果推断在线峰会&#xff0c;汇聚了国内外顶尖的因果推断领域专家、学者及业界精英&#xff0c;共同探讨因果推断的最新进展、应用与挑战。本文将…

Lambda表达式 --Java学习笔记

Lambda表达式是jdk8开始新增的一种语法形式作用&#xff1a;用于简化匿名内部类的代码写法格式&#xff1a; 注意&#xff1a;Lambda表达式只能简化函数式接口的匿名内部类 函数式接口指有且仅有一个抽象方法的接口 将来我们见到的大部分函数式接口&#xff0c;上面都可能会…

桥接模式(Bridge Pattern)

原文地址&#xff1a;https://jaune162.blog/design-pattern/bridge-pattern.html 更多精彩文章请移步&#xff1a;https://jaune162.blog 更多专题系列文章请移步&#xff1a;https://books.jaune162.blog 序言 桥接模式是一种结构型设计模式&#xff0c;它旨在将抽象部分与实…

vue/uniapp路由history模式下宝塔空间链接打开新窗口显示404解决方法

vue/uniapp路由history模式下宝塔空间链接打开新窗口显示404&#xff0c;或者域名后带路径参数刷新就报404 解决方法&#xff1a; 宝塔中站点配置修改&#xff1a;【配置文件】中添加下面代码&#xff0c;具体如图&#xff1a; location / {try_files $uri $uri/ /index.html…

Vue2+ElementUI表单、Form组件的封装

Vue2ElementUI表单、Form组件的封装 &#xff1a;引言 在 Vue2 项目中&#xff0c;ElementUI 的 el-form 组件是常用的表单组件。它提供了丰富的功能和样式&#xff0c;可以满足各种需求。但是&#xff0c;在实际开发中&#xff0c;我们经常会遇到一些重复性的需求&#xff0c…

ChatGPT解决hmm...something seems to have gone wrong.

ChatGPT解决hmm…something seems to have gone wrong. 这里是官方社区的一种workaround办法。仅仅只是mark一下。 我这边遇到的现象是&#xff0c;ChatGPT 3.5是正常的&#xff0c;但是使用ChatGPT 4就会频繁的出现这样的输出。而且恶心的是&#xff0c;即使是这种输出&…