java正則表達式之Pattern與Matcher類詳解
Pattern.split方法詳解
/** * 測試Pattern.split方法 */ @Test public void testPatternSplit() { String str = '{0x40, 0x11, 0x00, 0x00}'; // 分割符為:逗號, {,}, 空白符 String regex = '[,{}s]'; Pattern pattern = Pattern.compile(regex); /* * 1. split 方法用于使用正則表達式中的字符分割待匹配的字符串 * * 注意: * 1. 如果分割符位于原字符串的起始位置,則分割的時候,會在起始位置上分割出一個''出來 * 2. 如果有連續兩個分隔符,則會在這兩個分割符之間分割有一個''出來 * */ System.out.println('----------- split test -----------'); String[] results = pattern.split(str); System.out.println('length :' + results.length); for (int i = 0; i < results.length; i++) { System.out.println('element_' +i + ' :' + results[i]); } System.out.println(Arrays.toString(results)); /* * 2. split方法的limit參數的意思是使用正則表達式的分割字符將原字符串分為limit個組 * **/ System.out.println('n----------- split limit test -----------'); String[] resultsLimit = pattern.split(str, 2); for (int i = 0; i < resultsLimit.length; i++) { System.out.print(resultsLimit[i]); } }
結果:
----------- split test -----------element_0 :element_1 :0x40element_2 :element_3 :0x11element_4 :element_5 :0x00element_6 :element_7 :0x00[, 0x40, , 0x11, , 0x00, , 0x00]
----------- split limit test -----------0x40, 0x11, 0x00, 0x00}
Matcher的find/find/start/end方法詳解
測試Matcher的find方法:嘗試在目標字符串中查找下一個匹配的字串,需在循環中迭代。 groupCount :返回當前查找所獲得的匹配組的數量,不包括整個整個正則表達式的匹配。 比如,表達式有兩個子分組,則groupCount == 2 group(i):指的是用()包含的子分組,按照定義的順序標識下標,當正則表達式中使用 |連接分組,那么有的分組匹配的字串可能為null。 start(group):返回此子分組匹配的子串在原字符串中的起始位置(包含) end(group):返回此子分組匹配的子串在原字符串中的結束位置(不包含) 即子分組匹配的字符串在原字符串的位置為 [start(i),end(i)),左閉右開。@Test public void testMatcherGroupFindStartEnd() { String str = '{0x40, 0x31, 0x20, 0x00}'; String regex = '([A-Za-z0-9]+)(,)'; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); // 對于在整個原字符串中,找到的下一個匹配的字串 while (matcher.find()) { // 輸出groupCount的數量 System.out.println('groupCount : ' + matcher.groupCount()); // 0-輸出整個匹配 System.out.println('the substring of contains all group : ' + matcher.group(0)); System.out.println('group_0 start index : ' + matcher.start(0) + ' end :' + matcher.end(0)); // 依次輸出子分組的匹配結果 // 如果子分組之間是通過 | 來連接的,則子分組的匹配結果有的為null for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println('group_' + i + ':' + matcher.group(i)); System.out.println('group_' + i + ' start index : ' + matcher.start(i) + ' end :' + matcher.end(i)); } } }
結果:
groupCount : 2the substring of contains all group : 0x40,group_0 start index : 1 end :6group_1:0x40group_1 start index : 1 end :5group_2:,group_2 start index : 5 end :6groupCount : 2the substring of contains all group : 0x31,group_0 start index : 7 end :12group_1:0x31group_1 start index : 7 end :11group_2:,group_2 start index : 11 end :12groupCount : 2the substring of contains all group : 0x20,group_0 start index : 13 end :18group_1:0x20group_1 start index : 13 end :17group_2:,group_2 start index : 17 end :18
Matcher的replace/append方法詳解
測試Matcher的匹配替換以及追加的方法:
matcher.replaceAll方法 :替換在原字符串中所有被正則表達式匹配的字串,并返回替換之后的結果 matcher.replaceFirst方法 :替換在原字符串中第一個被正則表達式匹配的字串,并返回替換之后的結果 matcher.appendReplacement方法 : 將當前匹配子串替換為指定字符串,并且將替換后的子串以及其之前到上次匹配子串之后的字符串段添加到一個StringBuffer對象里(需while(matcher.find())進行配合迭代) matcher.appendTail(StringBuffer sb) 方法則將最后一次匹配工作后剩余的字符串添加到一個StringBuffer對象里。3和4的結合能夠實現將原字符串中的某些字串替換指定字符,并返回完成替換之后的結果
@Test public void testMatcherReplaceAppend() { String str = '{0x40, 0x31, 0x20, 0x00}'; String regex = '([0-9A-Za-z]+)'; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); // replaceAll System.out.println('----------- replace all test ----------'); String replacedAllStr = matcher.replaceAll('replace'); System.out.println('replaced : ' + replacedAllStr); //matcher.reset(str); // 重置被matcher的字符串 matcher.reset(); // 重置matcher,以實現對原字符串重新搜索 // replaceFirst System.out.println('------------ replace first test ---------'); String replacedFirstStr = matcher.replaceFirst('replace'); System.out.println('replaced first : ' + replacedFirstStr); matcher.reset(); // appendReplacement System.out.println('------------- appendReplacement test ------------'); StringBuffer appendRepStr = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(appendRepStr,'0xffff'); } System.out.println(appendRepStr); // 最后調用appendTail將匹配剩余的字符串添加都StringBuffer的末尾 // 注意這時要實現完整的功能:將所有匹配的內容替換并添加到appendRepStr中,剩余未匹配的繼續添加到 // appendRepStr中,相當于對原字符串進行全部的替換 // 此時要保證,在遍歷所有匹配的字串后調用appendTail方法 System.out.println('------------ appendTail test ---------------'); matcher.appendTail(appendRepStr); System.out.println(appendRepStr); }
結果:
----------- replace all test ----------replaced : {replace, replace, replace, replace}------------ replace first test ---------replaced first : {replace, 0x31, 0x20, 0x00}------------- appendReplacement test ------------{0xffff, 0xffff, 0xffff, 0xffff------------ appendTail test ---------------{0xffff, 0xffff, 0xffff, 0xffff}
測試文件源碼地址
https://github.com/zhanglbjames/exercises/blob/master/src/test/java/huawei_8_16/TestT1.java
1-匹配字符類
方括號一次只能匹配括號內的一個字符
[abc]
a, b, or c (簡單類)
[^abc]
除了a、b或c之外的任意 字符(求反)
[a-zA-Z]
a到z或A到Z ,包含(范圍)
[a-zA-Z0-9]
匹配一次所有數字和字母的類型
[a-b-r]
匹配 a-b ,連接符 -,r注意如果想要連接符起到范圍的作用,應該滿足如下格式[a-bc-de-gf-k]即每個連接符占用的字符不能被其他連接符占用
圓括號一次匹配多個字符
比如方括號的正則表達式't[aeio]n'只匹配'tan'、'Ten'、'tin'和'ton',只能匹配單個字符,不能匹配'taeion' 圓括號可以一次匹配多個字符,比如使用't(a|e|i|o|oo)n'正則表達式,可以匹配'taen','tan','taeiooon'等 也可以配合[]使用,如正則為't([aei]|o|oo)n',則可以匹配 'taon','teoon',但是不能匹配'taeioon'2-預定義字符類
. 匹配除換行符 n 之外的任何單字符
比如:表達式就是“t.n”,它匹配“tan”、“ten”、“tin”和“ton”,還匹配“t#n”、“tpn”甚至“t n”。
d 數字: [0-9] D 非數字: [^0-9] s 空格符: [ tnx0Bfr] S 非空格符: [^s] w 單詞字符: [a-zA-Z_0-9] W 非單詞字符: [^w]記憶規則 大寫表示取反,小寫如下記憶
d:digit(數字)s:space(空白字符)w:word(單詞字符), 注意包括下劃線
3-表達式匹配次數
* 0次或者多次(允許0次的貪婪匹配)
例如,zo* 能匹配 'z' 以及 'zoo'。* 等價于{0,}
+ 1次或者多次(貪婪匹配)
例如,’zo+’ 能匹配 'zo' 以及 'zoo',但不能匹配 'z'。+ 等價于 {1,}。
? 0次或者1次 (非貪婪匹配,允許0次)
例如,'do(es)?' 可以匹配 'do' 或 'does' 。? 等價于 {0,1}。
{n} 恰好n次
{n,m} 從n次到m次
{n,} 至少n次
注意上述n為非負數
4-特殊字符需要轉義
總結
到此這篇關于java正則表達式之Pattern與Matcher類的文章就介紹到這了,更多相關java正則表達式Pattern與Matcher類內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
