最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

java正則表達(dá)式的使用

2022-08-09 09:26 作者:虛云幻仙  | 我要投稿

/**
* 測(cè)試正則表達(dá)式
*/

public class RegularExpression1 {
? ?public static void main(String[] args) {
? ? ? ?String re = "[a]";
? ? ? ?//正則表達(dá)式使用字符串書寫,用[]限定匹配的內(nèi)容,用{}限定長(zhǎng)度,當(dāng)只有[]沒(méi)有{}時(shí)限定長(zhǎng)度為1
? ? ? ?String content = "a";
? ? ? ?System.out.println(content.matches(re));
? ? ? ?//對(duì)要操作的字符串調(diào)用.matches(String regex)方法,將正則表達(dá)式傳入,返回boolean該字符串是否符合正則表達(dá)式,結(jié)果為true
? ? ? ?content = "aa";
? ? ? ?System.out.println(content.matches(re));
? ? ? ?//因?yàn)閞e限定字符串長(zhǎng)度為1,所以結(jié)果為false
? ? ? ?re = "[a-z]";
? ? ? ?//[a-z]匹配所有小寫字母
? ? ? ?content = "ab";
? ? ? ?System.out.println(content.matches(re));
? ? ? ?//結(jié)果為false,因?yàn)橄薅俗址L(zhǎng)度為1
? ? ? ?re = "[a-zA-Z0-9]";
? ? ? ?//需要匹配的字符都寫進(jìn)[]內(nèi)并且不用分隔
? ? ? ?content = "2";
? ? ? ?System.out.println(content.matches(re));
? ? ? ?//結(jié)果為true
? ? ? ?re = "[123457-9]{3}";
? ? ? ?//匹配數(shù)字12345789,長(zhǎng)度必須為3
? ? ? ?System.out.println(content.matches(re)+"false");
? ? ? ?content = "188";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "[0-9]{2,3}";
? ? ? ?//{2,3}限定長(zhǎng)度最小為2最大為3,這里包頭包尾
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "[0-9]{2,}";
? ? ? ?//{2,}第二個(gè)數(shù)空著表示不限最大長(zhǎng)度
? ? ? ?content = "434134132";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "[0-9]{0,}";
? ? ? ?//最小長(zhǎng)度不能空著,{0,表示允許匹配內(nèi)容長(zhǎng)度為0
? ? ? ?content = "";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "123456a-z{5}";
? ? ? ?//不加[]時(shí){5}只會(huì)匹配最后一個(gè)字符z,z前面的視為固定組合
? ? ? ?content = "123456a-zzzzz";
? ? ? ?System.out.println(content.matches(re));
? ? ? ?//123456a-和content的前半部分相匹配,z{5}和后面匹配,-和z也必須連著,結(jié)果為true
? ? ? ?//也就是說(shuō)[]內(nèi)無(wú)論寫多少內(nèi)容都是規(guī)定單個(gè)字符的取值范圍

? ? ? ?re = "[^abcde12345]";
? ? ? ?//[^為取反,限定匹配字符不能是[]內(nèi)的內(nèi)容
? ? ? ?content = "5";
? ? ? ?System.out.println(content.matches(re)+"false");
? ? ? ?re = "[0-9]?";
? ? ? ?//長(zhǎng)度限定符號(hào)?為限定長(zhǎng)度{0,1}
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?content = "a";
? ? ? ?System.out.println(content.matches(re));
? ? ? ?//雖然長(zhǎng)度為1符合{0,1}但內(nèi)容a不滿足0-9,結(jié)果為false
? ? ? ?re = "[0-9]*";
? ? ? ?//長(zhǎng)度限定符號(hào)*為限定長(zhǎng)度{0,}
? ? ? ?content = "";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "[0-9]+";
? ? ? ?//長(zhǎng)度限定符號(hào)+為限定長(zhǎng)度{1,}
? ? ? ?System.out.println(content.matches(re)+"false");
? ? ? ?re = "\\d";
? ? ? ?//預(yù)定義字符\d表示匹配數(shù)字[0-9] java字符串內(nèi)\為轉(zhuǎn)義字符,所以需要用\\來(lái)表示\,\d就變?yōu)榱薥\d
? ? ? ?content = "3";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "\\D";
? ? ? ?//大寫為匹配除了[0-9]以外的字符
? ? ? ?System.out.println(content.matches(re)+"false");
? ? ? ?re = "\\n";
? ? ? ?//預(yù)定義字符\n為匹配換行符,這里同樣需要兩個(gè)\來(lái)表示\,如果寫成\n會(huì)判定為字符串的換行符,\\n才是正則的預(yù)定義字符
? ? ? ?content = "\n";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "\\r";
? ? ? ?//預(yù)定義字符\r匹配回車符
? ? ? ?content = "\r";
? ? ? ?System.out.println(content.matches(re));
? ? ? ?re = "\\s";
? ? ? ?//預(yù)定義字符\s表示匹配任何空白字符,包括空格\s 制表符\t 換行符\n 回車\r等
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?content = "\n";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "\\S?";
? ? ? ?//大寫表示匹配除了空白字符以外的任何字符,包括中文
? ? ? ?content = "空";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "\\t+";
? ? ? ?//預(yù)定義字符\t匹配制表符
? ? ? ?content = "\t";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "\\w*";
? ? ? ?//預(yù)定義字符\w表示匹配字母、數(shù)字和下劃線{_a-zA-Z0-9}
? ? ? ?content = "_";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "\\W";
? ? ? ?//大寫為匹配除了\w以外的字符
? ? ? ?System.out.println(content.matches(re)+"false");
? ? ? ?//String類的方法.split()也是使用了正則表達(dá)式,所以當(dāng)遇到需要分割+*?時(shí)需要加\\來(lái)表示,直接寫+*?為判定為長(zhǎng)度限定符號(hào),\+會(huì)判定為轉(zhuǎn)義字符,或者也可以寫成[+],包在[]內(nèi)時(shí)會(huì)表示原意
? ? ? ?System.out.println(Arrays.toString("a+b*c?d".split("\\?")));
? ? ? ?//結(jié)果為[a+b*c, d]
? ? ? ?re = "[\\w空白字符]";
? ? ? ?//將預(yù)定義字符寫在[]內(nèi)會(huì)和括號(hào)內(nèi)的其他內(nèi)容一起組成限定范圍
? ? ? ?content = "空";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "[\\w&&[^a-z]]";
? ? ? ?//在[]內(nèi)添加&&[]會(huì)將&&前面內(nèi)容與&&[]中的內(nèi)容取交集,這里前面集合為{a-zA-Z0-9_}后面集合為非a-z,取交集為{A-Z0-9_}
? ? ? ?content = "a";
? ? ? ?System.out.println(content.matches(re)+"false");
? ? ? ?re = "[a-z]{1,2}\\d{4,5}";
? ? ? ?//組合定義,以字母a-z開頭并且字母長(zhǎng)度為1到2,后續(xù)接數(shù)字長(zhǎng)度為4-5
? ? ? ?content = "a1234";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?content = "ab54321";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?//這時(shí)單一的{}不再代表總長(zhǎng)度,每一組內(nèi)容限定[]的前后順序也必須相符,這里總長(zhǎng)度為5-7,但具體哪一位匹配什么還是要看表達(dá)式
? ? ? ?re = "\\d?\\W*";
? ? ? ?//開頭以1個(gè)或0個(gè)數(shù)字,后續(xù)限制內(nèi)容為\\W長(zhǎng)度不限
? ? ? ?content = "{:>?<";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "\\d[a-z]{2}";
? ? ? ?//\\d不加{}表示1位,后續(xù)兩位a-z
? ? ? ?content = "2ed";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "(\\d{3,4})-(\\d{7,8})";
? ? ? ?//正則表達(dá)式中()同運(yùn)算符(),括起來(lái)的部分優(yōu)先處理
? ? ? ?content = "010-53456166";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "()\\d{3,4}-\\d{7,8}";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "\\(\\d{3,4}\\)-\\d{7,8}";
? ? ? ?//想在正則中表示(括號(hào)同樣需要\\(
? ? ? ?System.out.println(content.matches(re)+"false");
? ? ? ?re = ".*";
? ? ? ?//預(yù)定義字符.代表匹配除了\n\r以外的任意字符
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?content = "\n";
? ? ? ?System.out.println(content.matches(re)+"false");
? ? ? ?re = "\\.*";
? ? ? ?//想表示.同樣需要\\.或者[.]在方括號(hào)內(nèi)不加\也可以
? ? ? ?content = "....";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "[a-d]|[x-z]";
? ? ? ?//或運(yùn)算| 內(nèi)容為前面集合的范圍后者后面集合的范圍
? ? ? ?content = "x";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "([a-d]|[x-z]){5}";
? ? ? ?//將前面用()括起來(lái)表示匹配a-d或x-z的字符,后面加{5}表示匹配5個(gè)該字符,這里每個(gè)字符單獨(dú)判定,允許a-d和x-z混合的情況
? ? ? ?content = "abxdy";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "[a-d]|[x-z]{5}";
? ? ? ?//如果沒(méi)有括起來(lái),{5}的作用范圍只影響[x-z],所以匹配范圍為一個(gè)a-d或者五個(gè)x-z
? ? ? ?System.out.println(content.matches(re)+"false");
? ? ? ?content = "c";
? ? ? ?System.out.println(content.matches(re)+"true");
? ? ? ?re = "[a-d]{5}|[x-z]{5}";
? ? ? ?//想要分開a-d和x-z只能單獨(dú)寫完整的限定
? ? ? ?content = "abcyx";
? ? ? ?System.out.println(content.matches(re)+"false");
? ? ? ?re = "^\\w*空+$";
? ? ? ?//在[]外使用的^,放在正則的開頭表示匹配限定以^后面的字符開頭,$放在正則的結(jié)尾表示以$前面的字符結(jié)尾
? ? ? ?content = "空";
? ? ? ?//^\\w*作為一組,表示以零個(gè)或多個(gè)\w開頭,空+$作為一組,表示以一個(gè)或多個(gè)空結(jié)尾,所以空前面沒(méi)有內(nèi)容也符合^\\w*的要求
? ? ? ?System.out.println(content.matches(re)+"true");

? ? ? ?//常見(jiàn)正則表達(dá)式
? ? ? ?//郵箱

? ? ? ?re = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
? ? ? ?re = "([\\w&&[^_]]+[-|.]?)+[\\w&&[^_]]@([a-z0-9A-Z]+(-[a-z\\dA-Z]+)?\\.)+[a-zA-Z]{2,}";
? ? ? ?//IP地址
? ? ? ?re = "(25[0-5])|(2[0-4]\\d)|([0-1]\\d{2})|([1-9]?\\d)";
? ? ? ?//URL
? ? ? ?re = "[a-zA-Z]+://\\S*";
? ? ? ?re = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?";
? ? ? ?//身份證號(hào)碼
? ? ? ?re = "^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})(\\d|X)$";
? ? ? ?re = "(^\\d{18}$)|(^\\d{15}$)";
? ?}
}

java正則表達(dá)式的使用的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
海丰县| 新津县| 贵港市| 汕尾市| 泽州县| 吉木乃县| 会宁县| 宜宾市| 乐至县| 嘉祥县| 保靖县| 永福县| 大竹县| 长兴县| 中西区| 综艺| 湖南省| 康保县| 通山县| 夏津县| 东海县| 昌宁县| 嫩江县| 揭东县| 商水县| 蚌埠市| 眉山市| 儋州市| 吴桥县| 叙永县| 云霄县| 沅陵县| 伊春市| 平邑县| 深泽县| 改则县| 泽州县| 景宁| 黄骅市| 克山县| 东台市|