正則表達式
![image-20251104140210317]()
![image-20251104163925972]()
package Basic.src.com.Regex;
public class RegexDemo1 {
public static void main(String[] args) {
/*校驗QQ號是否正確
* 規則:6位及20位之內,0不能在開頭,必須全部是數字
* 先使用目前所學的知識完成校驗需求
* 然后體驗一下正則表達式*/
String qq = "1234567890";
//System.out.println(checkQQ(qq));
System.out.println(qq.matches("[1-9]\\d{5,19}"));
}
public static boolean checkQQ(String qq) {
//規則:6位及20位之內,0不能在開頭,必須全部是數字
//核心思想:
//先把異常數據進行過濾
//下面的就是滿足要求的數據了。
int len = qq.length();
if (len < 6||len>20){//先異常判斷
return false;
}
//0不能開頭
if(qq.startsWith("0")){
return false;
}
//必須全部是數字
for (int i = 0; i < qq.length(); i++) {
char c = qq.charAt(i);//charAt(i) 是 String 類的一個實例方法,作用是獲取字符串中指定索引位置的字符。
if (c < '0' || c > '9') {//字符單引號比較,比較的是字符的ASCII碼值
return false;
}
}
return true;
}
}
package Basic.src.com.Regex;
public class RegexDemo2 {
public static void main(String[] args) {
//只能是abc
System.out.println("==========1===========");
System.out.println("a".matches("[abc]"));//true
System.out.println("z".matches("[abc]"));//false
System.out.println("ab".matches("[abc]"));//false,意思是[]中的內容只能出現一個
System.out.println("ab".matches("[abc][abc]"));//true
//不能出現abc
System.out.println("==========2===========");
System.out.println("a".matches("[^abc]"));
System.out.println("z".matches("[^abc]"));
System.out.println("zz".matches("[^abc]"));
System.out.println("zz".matches("[^abc][^abc]"));
//a到z,A到Z(包括頭尾的范圍)
System.out.println("==========3===========");
System.out.println("a".matches("[a-zA-z]"));//true
System.out.println("z".matches("[a-zA-z]"));//true
System.out.println("aa".matches("[a-zA-z]"));//false
System.out.println("zz".matches("[a-zA-z]"));//false
System.out.println("zz".matches("[a-zA-z][a-zA-z]"));//true
System.out.println("0".matches("[a-zA-z]"));//false
System.out.println("0".matches("[a-zA-z0-9]"));//true
//[a-d[m-p]]
System.out.println("==========4===========");
System.out.println("a".matches("[a-z[m-p]]"));//true
System.out.println("d".matches("[a-z[m-p]]"));//true
System.out.println("m".matches("[a-z[m-p]]"));//true
System.out.println("p".matches("[a-z[m-p]]"));//true
System.out.println("e".matches("[a-z[m-p]]"));//false
System.out.println("0".matches("[a-z[m-p]]"));//false
//[a-z&&[^bc]] a-z和def的交集。為:d,e,f
//細節:如果要求兩個范圍的交集需要寫兩個符號&&
//如果寫成了一個&,那么此時的&就表示的不是交集了,而是簡簡單單的&符號,沒有任何含義
System.out.println("==========5===========");
System.out.println("a".matches("[a-z&[def]]"));//true
System.out.println("&".matches("[a-z&[def]]"));//true
System.out.println("&".matches("[a-z&&[def]]"));//false
System.out.println("d".matches("[a-z&&[def]]"));//true
System.out.println("a".matches("[a-z&&[def]]"));//false
//[a-z&&[^bc]] a-z和非bc的交集。(等同于[ad-z])
System.out.println("==========6===========");
System.out.println("a".matches("[a-z&&[^bc]]"));//true
System.out.println("b".matches("[a-z&&[^bc]]"));//false
System.out.println("0".matches("[a-z&&[^bc]]"));//false
//[a-z&&[^m-p]] a到z和除了m到p的交集,等同于[a-lq-z]
System.out.println("==========6===========");
System.out.println("a".matches("[a-z&&[^m-p]]"));//true
System.out.println("m".matches("[a-z&&[^m-p]]"));//false
System.out.println("0".matches("[a-z&&[^m-p]]"));//false
}
}
package Basic.src.com.Regex;
public class RegexDemo3 {
public static void main(String[] args) {
// \轉義字符 改變后面那個字符原本的含義
//練習:以字符串的形式打印一個雙引號
//此時這里的\是轉義字符,改變了后面那個''雙引號原本的含義
//把它變成了一個普通的雙引號而已
// \\第一個\是轉義字符,改變了后面那個\原本的含義
System.out.println("\"");
//.表示任意一個字符
System.out.println("你".matches("."));//true
System.out.println("你".matches(".."));//false
System.out.println("你a".matches(".."));//true
// \d表示任意的一個數字,所以\\d在java中要這么表示
//簡單記就是兩個\\表示一個\
System.out.println("a".matches("\\d"));//false
System.out.println("3".matches("\\d"));//true
System.out.println("333".matches("\\d"));//false
System.out.println("333".matches("\\d\\d\\d"));//true
// \\w只能是單詞字符[a-zA-Z_0-9]
System.out.println("z".matches("\\w"));//true
System.out.println("2".matches("\\w"));//true
System.out.println("21".matches("\\w"));//false
System.out.println("你".matches("\\w"));//false
System.out.println("_".matches("\\w"));//true
// \\W非單詞字符
System.out.println("你".matches("\\W"));//true
System.out.println("======================================");
//必須是數字 字母 下劃線 至少六位
System.out.println("2442fsfsf".matches("\\w{6,}"));//true
System.out.println("24sf".matches("\\w{6,}"));//false
//必須是數字和字符 必須是四位
System.out.println("23dF".matches("a-zA-Z0-9{4}"));//true
System.out.println("23_F".matches("a-zA-Z0-9{4}"));//false
System.out.println("23dF".matches("[\\w&&[^_]]{4}"));//true
System.out.println("23_F".matches("[\\w&&[^_]]{4}"));//false
}
}
package Basic.src.com.Regex;
public class RegexDemo4 {
public static void main(String[] args) {
/*
* 需求
* 13112345678 13712345667 13945679027 139456790271
* 020-2324242 02122442 027-42424 0712-3242434
* 3232323@qq.com zhangsan@itcast.cnn dlei0009@163.com dlei0009@pci.com.cn
* */
//心得:從左到右一個個看
//第一部分:1表示手機號碼只能以1開頭
//第二部分:[3-9],表示手機號碼第二位只能是3-9之間
//第三部分:\\d{9} 表示任意數字可以出現且僅可以出現9次
System.out.println("================手機號碼====================");
String regex1 = "1[3-9]\\d{9}";
System.out.println("13112345678".matches(regex1));
System.out.println("13712345667".matches(regex1));
System.out.println("13945679027".matches(regex1));
System.out.println("139456790271".matches(regex1));
System.out.println("================座機號碼====================");
//一:區號 0[1-9]{2,3}
//0表示一定是以0開頭
//[1-9]{2,3}:表示區號從第二位開始可以是任意的1-9,且可以出現2-3次
//二:-?次數0次或1次
//三:總長度5-10位
String regex2 = "0[1-9]{2,3}-?[1-9]\\d{4,9}";
System.out.println("020-2324242".matches(regex2));
System.out.println("02122442".matches(regex2));
System.out.println("027-42424".matches(regex2));
System.out.println("0712-3242434".matches(regex2));
//3232323@qq.com zhangsan@itcast.cnn dlei0009@163.com dlei0009@pci.com.cn
//第一部分:@的左邊 \\w+:任意的字母或者下劃線至少可以出現一次
// \\w:[a-z A-Z 0-9]
//第二部分:@只能出現一次
//第三部分:再分三小段
//3.1 .的左邊[\\w&&[^_]]{2,6}表示任意字符去掉下劃線,總共出現2-6次
//3.2 .需要轉義\\.第一個\轉義了第二個,使得第二個才是轉義.的 \\.
//3..3 大寫字母跟小寫字母都可以,只能出現2-3次 [a-zA-Z]{2,3}
//把3.2和3.3看作一組,可以反復出現1-2次
System.out.println("================郵箱====================");
String regex3 = "\\w+@[\\w&&[^_]]{2,6}(\\.[a-zA-Z]{2,3}){1,2}";
System.out.println("3232323@qq.com".matches(regex3));
System.out.println("zhangsan@itcast.cnn".matches(regex3));
System.out.println("dlei0009@163.com".matches(regex3));
System.out.println("dlei0009@pci.com.cn".matches(regex3));
//實際開發中很少會自己寫正則表達式
//百度一個類似的,改成自己想要的
//String regex4 = "/^(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d$/";^和$表示從開頭到末尾,但是java的matchs是默認的 ?:
String regex4 = "([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d";
//[01]\d|2[0-3] |是或者的意思
System.out.println("23:11:11".matches(regex4));
System.out.println("00:00:00".matches(regex4));
String regex5 = "([01]\\d|2[0-3])(:[0-5]\\d){2}";
System.out.println("23:11:11".matches(regex5));
}
}