String字串的replace函式和正則表示式
package zhengze; public class sss { public static void main(String[] args) { // TODO Auto-generated method stub String str="hello,java!"; System.out.println(str.replace("\\w", "#")); System.out.println(str.replace("\\w*", "#")); System.out.println(str.replace("\\w*?", "#")); //首先\\w是代表匹配所有數字字母下劃線,沒有表示數量則是代表一個字元。 //然後*+?這三個是正則特殊字元中,表示數量。*0或多,+1或多,?0或1 //算是{n} {n,} {n,m}這三個表示數量的。這樣,幾乎所有數量都可以表示了。 //這裡我叫它們是正則數量符,這樣總共有6種數量符。 //數量表示符又有三種模式,貪婪模式,勉強模式,佔用模式。最後兩個表示式一樣。 //所以這裡我就介紹下貪婪模式和勉強模式,前者除非條件固定,那麼會一直匹配下去 //後者除非條件固定,那兒會匹配最少的字元 //貪婪模式是表示式加一個任意的數量符,勉強模式是貪婪模式加一個?數量符 System.out.println("\r\n以下測試的是replaceFirst函式"); System.out.println("普通匹配,沒有任何模式"+str.replaceFirst("\\w", "#")); System.out.println("0或多的貪婪模式"+str.replaceFirst("\\w*", "#")); System.out.println("0或多的勉強模式"+str.replaceFirst("\\w*?", "#")); System.out.println("2位的貪婪模式"+str.replaceFirst("\\w{2}", "#")); System.out.println("2位的勉強模式"+str.replaceFirst("\\w{2}?", "#")); System.out.println("大於2位的貪婪模式"+str.replaceFirst("\\w{2,}", "#")); System.out.println("大於2位的勉強模式"+str.replaceFirst("\\w{2,}?", "#")); System.out.println("2到4位的貪婪模式"+str.replaceFirst("\\w{2,4}", "#")); System.out.println("2到4位的勉強模式"+str.replaceFirst("\\w{2,4}?", "#")); System.out.println("\r\n以下測試的是replaceAll函式"); System.out.println("普通匹配,沒有任何模式"+str.replaceAll("\\w", "#")); System.out.println("0或多的貪婪模式"+str.replaceAll("\\w*", "#")); System.out.println("0或多的勉強模式"+str.replaceAll("\\w*?", "#")); System.out.println("2位的貪婪模式"+str.replaceAll("\\w{2}", "#")); System.out.println("2位的勉強模式"+str.replaceAll("\\w{2}?", "#")); System.out.println("大於2位的貪婪模式"+str.replaceAll("\\w{2,}", "#")); System.out.println("大於2位的勉強模式"+str.replaceAll("\\w{2,}?", "#")); System.out.println("2到4位的貪婪模式"+str.replaceAll("\\w{2,4}", "#")); System.out.println("2到4位的勉強模式"+str.replaceAll("\\w{2,4}?", "#")); System.out.println("\r\n以下單獨測試的是replaceAll函式,0或多的貪婪模式"); System.out.println("java的結果是"+"java".replaceAll("\\w*", "#")); System.out.println("java,的結果是"+"java,".replaceAll("\\w*", "#")); System.out.println("java,,的結果是"+"java,,".replaceAll("\\w*", "#")); System.out.println(",java,,的結果是"+",java,,".replaceAll("\\w*", "#")); } }
首先介紹一下知識點。字串的三個替換方法是如程式所示。有三種。
輸入結果是
hello,java!
hello,java!
hello,java!
以下測試的是replaceFirst函式
普通匹配,沒有任何模式#ello,java!
0或多的貪婪模式#,java!
0或多的勉強模式#hello,java!
2位的貪婪模式#llo,java!
2位的勉強模式#llo,java!
大於2位的貪婪模式#,java!
大於2位的勉強模式#llo,java!
2到4位的貪婪模式#o,java!
2到4位的勉強模式#llo,java!
以下測試的是replaceAll函式
普通匹配,沒有任何模式#####,####!
0或多的貪婪模式##,##!#
0或多的勉強模式#h#e#l#l#o#,#j#a#v#a#!#
2位的貪婪模式##o,##!
2位的勉強模式##o,##!
大於2位的貪婪模式#,#!
大於2位的勉強模式##o,##!
2到4位的貪婪模式#o,#!
2到4位的勉強模式##o,##!
以下測試的是replaceAll函式,0或多的貪婪模式
java的結果是##
java,的結果是##,#
java,,的結果是##,#,#
,java,,的結果是#,##,#,#
|
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".
|
|
String
java.lang.String.replaceFirst(String
regex, String replacement)
Replaces the first substring of this string that matches the given regular expression with the given replacement. An invocation of this method of the form str Note that backslashes (
|
|
String
java.lang.String.replaceAll(String
regex, String replacement)
Replaces each substring of this string that matches the given regular expression with the given replacement. An invocation of this method of the form str Note that backslashes (
|
需要指出,只有使用了數量表達式,那麼一定是三種模式中的一種。
//首先\\w是代表匹配所有數字字母下劃線,沒有表示數量則是代表一個字元。
//然後*+?這三個是正則特殊字元中,表示數量。*0或多,+1或多,?0或1
//算是{n} {n,} {n,m}這三個表示數量的。這樣,幾乎所有數量都可以表示了。
//這裡我叫它們是正則數量符,這樣總共有6種數量符。
//數量表示符又有三種模式,貪婪模式,勉強模式,佔用模式。最後兩個表示式一樣。
//所以這裡我就介紹下貪婪模式和勉強模式,前者除非條件固定,那麼會一直匹配下去
//後者除非條件固定,那兒會匹配最少的字元
//貪婪模式是表示式加一個任意的數量符,勉強模式是貪婪模式加一個?數量符
分析輸出結果
一。replace函式。因為引數是字串,而不識別正則表示式,所以不會進行任何替換。
二。replaceFirst函式。結果很好理解,可以見得,如果數量沒有被固定,那麼模式是能起到作用的。
三。replaceAll函式。這裡面稍微複雜一點,因為函式是要替換所有的符合要求的。0或多的貪婪模式,這個比較特殊,下面講。
0或多的勉強模式,其實就是所有0位的符合要求的,替換掉了,所以是那種輸出。
四。replaceAll函式,0或多的貪婪模式。這個比較特殊。可見是隻要是數字字母下劃線的集合(因為是貪婪),那麼就替換。然後就是隻要是“單詞”,那麼就替換為##,好像意思是單詞前有個0位單詞,只有0位,替換為#。然後是非匹配和非匹配或者首尾之間也有一個0位單詞,所以也得替換。