叨逼叨两句
- 今天看计算机理论书和视频看high了,一瞬间,我突然明白,其实所谓娱乐真的没有固定形式,只要身心愉悦即可
- 刷剧看电影玩游戏如果不能让你内心愉悦,就无法算做愉悦,而如果看一些自己想看的书和视频,哪怕只是一些专业上的理论书,只要你有兴趣,有需求,它就是一种娱乐。
- 创造与思考真的就是少数人的娱乐,刷剧看电影玩游戏才是大多数人的娱乐。前者不是谁都能感受到【刺激的是你的理性自我】,后者谁都能感受到【刺激的是你的本能与感性自我】。
- 图灵真是我的偶像啊,图灵机模型真是精妙,据说是一次长跑后想出来的,看来锻炼果然是好习惯。
- 看了纸牌屋,深深感受到,找伴侣,果然还是得找理性自我和感性自我能交流的,这样老了才有足够的愉悦感,光是本能自我的快乐,也就撑得住那么一小段荷尔蒙肆虐的时光而已。
14-(1-10):正则表达式
概述
- 是一个用来描述或者匹配一系列符合某个语法规则的字符串,它是一种规则。
作用
用来匹配字符串
作用演示
不使用正则表达式
package com.test.regex;public class Demo01 { public static void main(String[] args) { //不使用正则表达式 String qq = "111111111"; System.out.println(checkQQ(qq)); } public static boolean checkQQ(String qq) { boolean flag = true; if((qq.length()>=5)&&(qq.length()<=15)){ if(!qq.startsWith("0")){ char[] arr = qq.toCharArray(); for(int i = 0; i < arr.length; i++){ if(!(arr[i] >= '0' && arr[i] <= '9')){ flag = false; break; } } } else { flag = false; } } else { flag = false; } return flag; }}
使用正则表达式
package com.test.regex;public class Demo01 { public static void main(String[] args) { //使用正则表达式 String qq = "111111111"; String regex = "[1-9]\\d{4,14}"; System.out.println(qq.matches(regex)); }
正则表达式——字符类
[abc]
[^abc]
[a-zA-Z]
[c-f[M-P]]
[a-z&&[def]]
[a-z&&[^bc]]
[a-z&&[^m-p]]
package com.test.regex;public class Demo02 { public static void main(String[] args) { //demo01(); //demo02(); //demo03(); //demo04(); //demo05(); //demo06(); //demo07(); } public static void demo07() { String regex = "[a-z&&[^m-p]]"; //相当于将m-p从a-z中减去 System.out.println("c".matches(regex)); // true System.out.println("m".matches(regex)); // false } public static void demo06() { String regex = "[a-z&&[^bc]]"; //相当于将bc从a-z中减去 System.out.println("c".matches(regex)); // false System.out.println("d".matches(regex)); // true } public static void demo05() { String regex = "[a-z&&[def]]"; //等同于[def],交集 System.out.println("c".matches(regex)); // false System.out.println("d".matches(regex)); // true } public static void demo04() { String regex = "[c-f[M-P]]"; //代表c-f和M-P范围内任意一个单字符,这是一个并集 System.out.println("b".matches(regex)); // false System.out.println("c".matches(regex)); // true System.out.println("d".matches(regex)); // true System.out.println("L".matches(regex)); // false System.out.println("M".matches(regex)); // true System.out.println("N".matches(regex)); // true System.out.println("1".matches(regex)); // false System.out.println("&".matches(regex)); // false } public static void demo03() { String regex = "[a-zA-Z]"; //代表a-z和A-Z范围内任意一个单字符 System.out.println("a".matches(regex)); // true System.out.println("z".matches(regex)); // true System.out.println("A".matches(regex)); // true System.out.println("Z".matches(regex)); // true System.out.println("1".matches(regex)); // false System.out.println("&".matches(regex)); // false } public static void demo02() { String regex = "[^abc]"; //代表除了abc以外的任意一个单字符 System.out.println("a".matches(regex)); // false System.out.println("b".matches(regex)); // false System.out.println("c".matches(regex)); // false System.out.println("d".matches(regex)); // true System.out.println("1".matches(regex)); // true System.out.println("&".matches(regex)); // true // !!!这个之所以为false是因为它不是单个字符 System.out.println("10".matches(regex)); // false } public static void demo01() { String regex = "[abc]"; //代表abc中任意一个单字符 System.out.println("a".matches(regex)); // true System.out.println("b".matches(regex)); // true System.out.println("c".matches(regex)); // true System.out.println("d".matches(regex)); // false System.out.println("1".matches(regex)); // false System.out.println("&".matches(regex)); // false }}
正则表达式——预定义字符类
.
\d
\D
\s
\S
\w
\W
""
既不是空白字符,也不是非空白字符\\d
中第一个\
是转义字符,想要表示正则表达式\d
,需要用\\
转义出一个\
来。
package com.test.regex;public class Demo03 { public static void main(String[] args) { //demo01(); //demo02(); //demo03(); //demo04(); //demo05(); //demo06(); //demo07(); } public static void demo07() { String regex = "[\\W]"; //等同于[^\\w] System.out.println("a".matches(regex)); // false System.out.println("A".matches(regex)); // false System.out.println("_".matches(regex)); // false System.out.println("9".matches(regex)); // false System.out.println("&".matches(regex)); // true } public static void demo06() { String regex = "\\w"; //等同于[a-zA-Z_0-9] System.out.println("a".matches(regex)); // true System.out.println("A".matches(regex)); // true System.out.println("_".matches(regex)); // true System.out.println("9".matches(regex)); // true System.out.println("&".matches(regex)); // false } public static void demo05() { String regex = "\\S";// 非空白字符,等同于[^\\s] //""既不是空白字符,也不是非空白字符,所以为false System.out.println("".matches(regex)); // false System.out.println("a".matches(regex)); // true System.out.println(" ".matches(regex)); // false } public static void demo04() { String regex = "\\s"; //空白字符[ \\t\\n\\r\\x0B\\f] //""既不是空白字符,也不是非空白字符,所以为false System.out.println("".matches(regex));//false //下面的语句结果为false是因为我输入的是空格 System.out.println(" ".matches(regex));//true //下面的语句结果为true是因为我输入的是制表符tab System.out.println(" ".matches(regex));//false } public static void demo03() { String regex = "\\D";//等同于[^0-9] System.out.println("5".matches(regex));// false System.out.println("d".matches(regex));// true } public static void demo02() { String regex = "\\d"; // 等同于数组[0-9] System.out.println("5".matches(regex)); // true System.out.println("d".matches(regex)); // false } public static void demo01() { String regex = "."; System.out.println("a".matches(regex)); String regex2 = ".."; System.out.println("..".matches(regex2)); }}
正则表达式——数量词
- X? X一次或一次也没有
- X* X零次到多次(包含一次)
- X+ X一次到多次
- X{n} X恰好n次
- X{n,} X至少n次
- X{n,m} X至少n次,但是不超过m次
package com.test.regex;public class Demo04 { public static void main(String[] args) { //demo01(); //demo02(); //demo03(); //demo04(); //demo05(); //demo06(); } public static void demo06() { String regex = "[abc]{5,10}";//带表出现次数为[5,10],每一次出现都从abc里选一个 System.out.println("abcabcabc".matches(regex)); //true System.out.println("abc".matches(regex)); //false System.out.println("abcabcabcabc".matches(regex)); //false } public static void demo05() { String regex = "[abc]{5,}"; //代表至少出现5次单个字符,每一次出现都从abc里选一个 System.out.println("abcabcabc".matches(regex));// true System.out.println("abc".matches(regex));// false } public static void demo04() { String regex = "[abc]{5}"; //代表出现5次单个字符,每一次出现都从abc里选一个 System.out.println("aaaaa".matches(regex));// true System.out.println("ddddd".matches(regex));// false System.out.println("abcab".matches(regex)); // true } public static void demo03() { String regex = "[abc]+"; //代表abc中任意一个字符出现一次到多次 System.out.println("a".matches(regex));// true System.out.println("d".matches(regex));// false System.out.println("".matches(regex)); // false System.out.println(" ".matches(regex)); // false System.out.println("ab".matches(regex)); //true System.out.println("aaaaaaab".matches(regex)); //true System.out.println("abcabcabbcc".matches(regex)); //true } public static void demo02() { String regex = "[abc]*"; //代表abc中任意一个字符出现零次到多次 System.out.println("a".matches(regex));// true System.out.println("d".matches(regex));// false System.out.println("".matches(regex)); // true System.out.println(" ".matches(regex)); // false System.out.println("ab".matches(regex)); //true System.out.println("aaaaaaab".matches(regex)); //true System.out.println("abcabcabbcc".matches(regex)); //true } public static void demo01() { String regex = "[abc]?"; //代表abc中任意一个字符出现一次或者一次都不出现 System.out.println("a".matches(regex));// true System.out.println("d".matches(regex));// false System.out.println("".matches(regex)); // true System.out.println(" ".matches(regex)); // false System.out.println("ab".matches(regex)); // false }}
正则表达式的分割功能
- String[] split(String regex)
- .转义时必须
\\.
才行
package com.test.regex;public class Demo05 { public static void main(String[] args) { String s = "你好 我叫 南柯"; String[] arr = s.split(" "); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } System.out.println("---------------"); String s1 = "你好.我叫.南柯"; String[] arr1 = s1.split("\\."); for (int i = 0; i < arr1.length; i++) { System.out.println(arr1[i]); } }}