Java笔记(14):常用对象--正则表达式、GC
1、如何校验一个QQ号码案例
1 package cn.itcast_01; 2 3 import java.util.Scanner; 4 5 /* 6 * 校验qq号码. 7 * 1:要求必须是5-15位数字 8 * 2:0不能开头 9 * 10 * 分析: 11 * A:键盘录入一个QQ号码 12 * B:写一个功能实现校验 13 * C:调用功能,输出结果。 14 */ 15 public class RegexDemo { 16 public static void main(String[] args) { 17 // 创建键盘录入对象 18 Scanner sc = new Scanner(System.in); 19 System.out.println("请输入你的QQ号码:"); 20 String qq = sc.nextLine(); 21 22 System.out.println("checkQQ:"+checkQQ(qq)); 23 } 24 25 /* 26 * 写一个功能实现校验 两个明确: 明确返回值类型:boolean 明确参数列表:String qq 27 */ 28 public static boolean checkQQ(String qq) { 29 boolean flag = true; 30 31 // 校验长度 32 if (qq.length() >= 5 && qq.length() <= 15) { 33 // 0不能开头 34 if (!qq.startsWith("0")) { 35 // 必须是数字 36 char[] chs = qq.toCharArray(); 37 for (int x = 0; x < chs.length; x++) { 38 char ch = chs[x]; 39 if (!Character.isDigit(ch)) { 40 flag = false; 41 break; 42 } 43 } 44 } else { 45 flag = false; 46 } 47 } else { 48 flag = false; 49 } 50 51 return flag; 52 } 53 }
使用正则表达式改进:
1 package cn.itcast_01; 2 3 import java.util.Scanner; 4 5 /* 6 * 正则表达式:符合一定规则的字符串。 7 */ 8 public class RegexDemo2 { 9 public static void main(String[] args) { 10 // 创建键盘录入对象 11 Scanner sc = new Scanner(System.in); 12 System.out.println("请输入你的QQ号码:"); 13 String qq = sc.nextLine(); 14 15 System.out.println("checkQQ:" + checkQQ(qq)); 16 } 17 18 public static boolean checkQQ(String qq) { 19 // String regex ="[1-9][0-9]{4,14}"; 20 // //public boolean matches(String regex)告知此字符串是否匹配给定的正则表达式 21 // boolean flag = qq.matches(regex); 22 // return flag; 23 24 //return qq.matches("[1-9][0-9]{4,14}"); 25 26 return qq.matches("[1-9]\\d{4,14}"); 27 } 28 }
正则表达式的规则:
A:字符
x 字符 x。举例:'a'表示字符a
\\ 反斜线字符。
\n 新行(换行)符 ('\u000A')
\r 回车符 ('\u000D')
B:字符类
[abc] a、b 或 c(简单类)
[^abc] 任何字符,除了 a、b 或 c(否定)
[a-zA-Z] a到 z 或 A到 Z,两头的字母包括在内(范围)
[0-9] 0到9的字符都包括
C:预定义字符类
. 任何字符。我的就是.字符本身,怎么表示呢? \.
\d 数字:[0-9]
\w 单词字符:[a-zA-Z_0-9]
在正则表达式里面组成单词的东西必须有这些东西组成
D:边界匹配器
^ 行的开头
$ 行的结尾
\b 单词边界
就是不是单词字符的地方。
举例:hello world?haha;xixi
E:Greedy 数量词
X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n 次
X{n,} X,至少 n 次
X{n,m} X,至少 n 次,但是不超过 m 次
2、正则表达式的判断功能
1 package cn.itcast_02; 2 3 import java.util.Scanner; 4 5 /* 6 * 判断功能 7 * String类的public boolean matches(String regex) 8 * 9 * 需求: 10 * 判断手机号码是否满足要求? 11 * 12 * 分析: 13 * A:键盘录入手机号码 14 * B:定义手机号码的规则 15 * 13436975980 16 * 13688886868 17 * 13866668888 18 * 13456789012 19 * 13123456789 20 * 18912345678 21 * 18886867878 22 * 18638833883 23 * C:调用功能,判断即可 24 * D:输出结果 25 */ 26 public class RegexDemo { 27 public static void main(String[] args) { 28 //键盘录入手机号码 29 Scanner sc = new Scanner(System.in); 30 System.out.println("请输入你的手机号码:"); 31 String phone = sc.nextLine(); 32 33 //定义手机号码的规则 34 String regex = "1[38]\\d{9}"; 35 36 //调用功能,判断即可 37 boolean flag = phone.matches(regex); 38 39 //输出结果 40 System.out.println("flag:"+flag); 41 } 42 }
邮箱验证案例:
1 package cn.itcast_02; 2 3 import java.util.Scanner; 4 5 /* 6 * 校验邮箱 7 * 8 * 分析: 9 * A:键盘录入邮箱 10 * B:定义邮箱的规则 11 * 1517806580@qq.com 12 * liuyi@163.com 13 * linqingxia@126.com 14 * fengqingyang@sina.com.cn 15 * fqy@itcast.cn 16 * C:调用功能,判断即可 17 * D:输出结果 18 */ 19 public class RegexTest { 20 public static void main(String[] args) { 21 //键盘录入邮箱 22 Scanner sc = new Scanner(System.in); 23 System.out.println("请输入邮箱:"); 24 String email = sc.nextLine(); 25 26 //定义邮箱的规则 27 //String regex = "[a-zA-Z_0-9]+@[a-zA-Z_0-9]{2,6}(\\.[a-zA-Z_0-9]{2,3})+"; 28 String regex = "\\w+@\\w{2,6}(\\.\\w{2,3})+"; 29 30 //调用功能,判断即可 31 boolean flag = email.matches(regex); 32 33 //输出结果 34 System.out.println("flag:"+flag); 35 } 36 }
3、正则表达式的分割功能
1 package cn.itcast_03; 2 3 import java.util.Scanner; 4 5 /* 6 * 分割功能 7 * String类的public String[] split(String regex) 8 * 根据给定正则表达式的匹配拆分此字符串。 9 * 10 * 举例: 11 * 百合网,世纪佳缘,珍爱网,QQ 12 * 搜索好友 13 * 性别:女 14 * 范围:"18-24" 15 * 16 * age>=18 && age<=24 17 */ 18 public class RegexDemo { 19 public static void main(String[] args) { 20 //定义一个年龄搜索范围 21 String ages = "18-24"; 22 23 //定义规则 24 String regex = "-"; 25 26 //调用方法 27 String[] strArray = ages.split(regex); 28 29 // //遍历 30 // for(int x=0; x<strArray.length; x++){ 31 // System.out.println(strArray[x]); 32 // } 33 34 //如何得到int类型的呢? 35 int startAge = Integer.parseInt(strArray[0]); 36 int endAge = Integer.parseInt(strArray[1]); 37 38 //键盘录入年龄 39 Scanner sc = new Scanner(System.in); 40 System.out.println("请输入你的年龄:"); 41 int age = sc.nextInt(); 42 43 if(age>=startAge && age<=endAge) { 44 System.out.println("你就是我想找的"); 45 }else { 46 System.out.println("不符合我的要求,gun"); 47 } 48 } 49 }
练习:
1 package cn.itcast_03; 2 3 /* 4 * 分割功能练习 5 */ 6 public class RegexDemo2 { 7 public static void main(String[] args) { 8 // 定义一个字符串 9 String s1 = "aa,bb,cc"; 10 // 直接分割 11 String[] str1Array = s1.split(","); 12 for (int x = 0; x < str1Array.length; x++) { 13 System.out.println(str1Array[x]); 14 } 15 System.out.println("---------------------"); 16 17 String s2 = "aa.bb.cc"; 18 String[] str2Array = s2.split("\\."); 19 for (int x = 0; x < str2Array.length; x++) { 20 System.out.println(str2Array[x]); 21 } 22 System.out.println("---------------------"); 23 24 String s3 = "aa bb cc"; 25 String[] str3Array = s3.split(" +"); 26 for (int x = 0; x < str3Array.length; x++) { 27 System.out.println(str3Array[x]); 28 } 29 System.out.println("---------------------"); 30 31 //硬盘上的路径,我们应该用\\替代\ 32 String s4 = "E:\\JavaSE\\day14\\avi"; 33 String[] str4Array = s4.split("\\\\"); 34 for (int x = 0; x < str4Array.length; x++) { 35 System.out.println(str4Array[x]); 36 } 37 System.out.println("---------------------"); 38 } 39 }
把字符串中的数字排序案例
1 package cn.itcast_03; 2 3 import java.util.Arrays; 4 5 /* 6 * 我有如下一个字符串:"91 27 46 38 50" 7 * 请写代码实现最终输出结果是:"27 38 46 50 91" 8 * 9 * 分析: 10 * A:定义一个字符串 11 * B:把字符串进行分割,得到一个字符串数组 12 * C:把字符串数组变换成int数组 13 * D:对int数组排序 14 * E:把排序后的int数组在组装成一个字符串 15 * F:输出字符串 16 */ 17 public class RegexTest { 18 public static void main(String[] args) { 19 // 定义一个字符串 20 String s = "91 27 46 38 50"; 21 22 // 把字符串进行分割,得到一个字符串数组 23 String[] strArray = s.split(" "); 24 25 // 把字符串数组变换成int数组 26 int[] arr = new int[strArray.length]; 27 28 for (int x = 0; x < arr.length; x++) { 29 arr[x] = Integer.parseInt(strArray[x]); 30 } 31 32 // 对int数组排序 33 Arrays.sort(arr); 34 35 // 把排序后的int数组在组装成一个字符串 36 StringBuilder sb = new StringBuilder(); 37 for (int x = 0; x < arr.length; x++) { 38 sb.append(arr[x]).append(" "); 39 } 40 //转化为字符串 41 String result = sb.toString().trim(); 42 43 //输出字符串 44 System.out.println("result:"+result); 45 } 46 }
4、正则表达式的替换功能
1 package cn.itcast_04; 2 3 /* 4 * 替换功能 5 * String类的public String replaceAll(String regex,String replacement) 6 * 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。 7 */ 8 public class RegexDemo { 9 public static void main(String[] args) { 10 // 定义一个字符串 11 String s = "helloqq12345worldkh622112345678java"; 12 13 // 我要去除所有的数字,用*给替换掉 14 // String regex = "\\d+"; 15 // String regex = "\\d"; 16 //String ss = "*"; 17 18 19 // 直接把数字干掉 20 String regex = "\\d+"; 21 String ss = ""; 22 23 String result = s.replaceAll(regex, ss); 24 System.out.println(result); 25 } 26 }
5、Pattern和Matcher的概述
1 package cn.itcast_05; 2 3 import java.util.regex.Matcher; 4 import java.util.regex.Pattern; 5 6 /* 7 * 获取功能 8 * Pattern和Matcher类的使用 9 * 10 * 模式和匹配器的基本使用顺序 11 */ 12 public class RegexDemo { 13 public static void main(String[] args) { 14 // 模式和匹配器的典型调用顺序 15 // 把正则表达式编译成模式对象 16 Pattern p = Pattern.compile("a*b"); 17 // 通过模式对象得到匹配器对象,这个时候需要的是被匹配的字符串 18 Matcher m = p.matcher("aaaaab"); 19 // 调用匹配器对象的功能 20 boolean b = m.matches(); 21 System.out.println(b); 22 23 //这个是判断功能,但是如果做判断,这样做就有点麻烦了,我们直接用字符串的方法做 24 String s = "aaaaab"; 25 String regex = "a*b"; 26 boolean bb = s.matches(regex); 27 System.out.println(bb); 28 } 29 }
6、正则表达式的获取功能
1 package cn.itcast_05; 2 3 import java.util.regex.Matcher; 4 import java.util.regex.Pattern; 5 6 /* 7 * 获取功能: 8 * 获取下面这个字符串中由三个字符组成的单词 9 * da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu? 10 */ 11 public class RegexDemo2 { 12 public static void main(String[] args) { 13 // 定义字符串 14 String s = "da jia ting wo shuo,jin tian yao xia yu,bu shang wan zi xi,gao xing bu?"; 15 // 规则 16 String regex = "\\b\\w{3}\\b"; 17 18 // 把规则编译成模式对象 19 Pattern p = Pattern.compile(regex); 20 // 通过模式对象得到匹配器对象 21 Matcher m = p.matcher(s); 22 // 调用匹配器对象的功能 23 // 通过find方法就是查找有没有满足条件的子串 24 // public boolean find() 25 // boolean flag = m.find(); 26 // System.out.println(flag); 27 // // 如何得到值呢? 28 // // public String group() 29 // String ss = m.group(); 30 // System.out.println(ss); 31 // 32 // // 再来一次 33 // flag = m.find(); 34 // System.out.println(flag); 35 // ss = m.group(); 36 // System.out.println(ss); 37 38 while (m.find()) { 39 System.out.println(m.group()); 40 } 41 42 // 注意:一定要先find(),然后才能group() 43 // IllegalStateException: No match found 44 // String ss = m.group(); 45 // System.out.println(ss); 46 } 47 }
7、System类中垃圾回收的方法gc()的讲解
System.gc()可用于垃圾回收。当使用System.gc()回收某个对象所占用的内存之前,通过要求程序调用适当的方法来清理资源。在没有明确指定资源清理的情况下,Java提高了默认机制来清理该对象的资源,就是调用Object类的finalize()方法。finalize()方法的作用是释放一个对象占用的内存空间时,会被JVM调用。而子类重写该方法,就可以清理对象占用的资源,该方法有没有链式调用,所以必须手动实现。
从程序的运行结果可以发现,执行System.gc()前,系统会自动调用finalize()方法清除对象占有的资源,通过super.finalize()方式可以实现从下到上的finalize()方法的调用,即先释放自己的资源,再去释放父类的资源。
但是,不要在程序中频繁的调用垃圾回收,因为每一次执行垃圾回收,jvm都会强制启动垃圾回收器运行,这会耗费更多的系统资源,会与正常的Java程序运行争抢资源,只有在执行大量的对象的释放,才调用垃圾回收最好
1 package cn.itcast_01; 2 3 public class Person { 4 private String name; 5 private int age; 6 7 public Person() { 8 super(); 9 } 10 11 public Person(String name, int age) { 12 super(); 13 this.name = name; 14 this.age = age; 15 } 16 17 public String getName() { 18 return name; 19 } 20 21 public void setName(String name) { 22 this.name = name; 23 } 24 25 public int getAge() { 26 return age; 27 } 28 29 public void setAge(int age) { 30 this.age = age; 31 } 32 33 @Override 34 public String toString() { 35 return "Person [name=" + name + ", age=" + age + "]"; 36 } 37 38 @Override 39 protected void finalize() throws Throwable { 40 System.out.println("当前的对象被回收了" + this); 41 super.finalize(); 42 } 43 44 }
1 package cn.itcast_01; 2 3 /* 4 * System类包含一些有用的类字段和方法。它不能被实例化。 5 * 6 * 方法: 7 * public static void gc():运行垃圾回收器。 8 * public static void exit(int status) 9 * public static long currentTimeMillis() 10 * public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length) 11 */ 12 public class SystemDemo { 13 public static void main(String[] args) { 14 Person p = new Person("赵雅芝", 60); 15 System.out.println(p); 16 17 p = null; // 让p不再指定堆内存 18 System.gc(); 19 } 20 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步