织梦网站最下面的网站建设去除,从化区建设网站,策划人网,做网站没有签合同可以退款吗文章目录 1、正则表达式1.1 常用1.2 字符串匹配#xff0c;提取#xff0c;分割 2、异常2.1 运行时异常2.2 编译时异常2.3 自定义异常2.3.1 自定义编译时异常2.3.2 自定义运行时异常 1、正则表达式
就是由一些特定的字符组成#xff0c;完成一个特定的规则 可以用来校验数据… 文章目录 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)));//false1.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 simpleDateFormatnew 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 (age0age150){System.out.println(年龄保存成功);}else{//throw 跑出去这个异常对象上边调用者才会接受到这个异常否则不会报错//throws 抛出方法内部的异常 用在方法上throw new AgeIllegailtyException1(/age is illegalityage isage);}}
}
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 (age0age150){System.out.println(年龄保存成功);}else{//throw 跑出去这个异常对象上边调用者才会接受到这个异常否则不会报错throw new AgeIllegalityException(/age is illegalityage isage);}}
}