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,,的结果是#,##,#,#

       
replace
                                
                         
String java. lang. String.replace( CharSequence target, CharSequence replacement)

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".

Parameters:
target The sequence of char values to be replaced
replacement The replacement sequence of char values
Returns:
The resulting string
Since:
1.5
              
replaceFirst
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.replaceFirst(regex, repl) yields exactly the same result as the expression

java.util.regex.Pattern.compile(regex).matcher(str).replaceFirst(repl)

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see java.util.regex.Matcher.replaceFirst. Use java.util.regex.Matcher.quoteReplacement to suppress the special meaning of these characters, if desired.

Parameters:
regex the regular expression to which this string is to be matched
replacement the string to be substituted for the first match
Returns:
The resulting String
Throws:
PatternSyntaxException - if the regular expression's syntax is invalid
Since:
1.4
See Also:
java.util.regex.Pattern
@spec
JSR-51
replaceAll
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.replaceAll(regex, repl) yields exactly the same result as the expression

java.util.regex.Pattern.compile(regex).matcher(str).replaceAll(repl)

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see Matcher.replaceAll. Use java.util.regex.Matcher.quoteReplacement to suppress the special meaning of these characters, if desired.

Parameters:
regex the regular expression to which this string is to be matched
replacement the string to be substituted for each match
Returns:
The resulting String
Throws:
PatternSyntaxException - if the regular expression's syntax is invalid
Since:
1.4
See Also:
java.util.regex.Pattern
@spec
JSR-51
 从以上可以看出。第一个函数的两个参数都是字符串,而第二第三个函数的第一个参数则是正则表达式,即是一种特殊的字符串。

需要指出,只有使用了数量表达式,那么一定是三种模式中的一种。

              //首先\\w是代表匹配所有数字字母下划线,没有表示数量则是代表一个字符。
         //然后*+?这三个是正则特殊字符中,表示数量。*0或多,+1或多,?0或1
         //算是{n} {n,} {n,m}这三个表示数量的。这样,几乎所有数量都可以表示了。
         //这里我叫它们是正则数量符,这样总共有6种数量符。
         //数量表示符又有三种模式,贪婪模式,勉强模式,占用模式。最后两个表示式一样。
         //所以这里我就介绍下贪婪模式和勉强模式,前者除非条件固定,那么会一直匹配下去
         //后者除非条件固定,那儿会匹配最少的字符
         //贪婪模式是表示式加一个任意的数量符,勉强模式是贪婪模式加一个?数量符
分析输出结果

一。replace函数。因为参数是字符串,而不识别正则表达式,所以不会进行任何替换。

二。replaceFirst函数。结果很好理解,可以见得,如果数量没有被固定,那么模式是能起到作用的。

三。replaceAll函数。这里面稍微复杂一点,因为函数是要替换所有的符合要求的。0或多的贪婪模式,这个比较特殊,下面讲。

        0或多的勉强模式,其实就是所有0位的符合要求的,替换掉了,所以是那种输出。

四。replaceAll函数,0或多的贪婪模式。这个比较特殊。可见是只要是数字字母下划线的集合(因为是贪婪),那么就替换。然后就是只要是“单词”,那么就替换为##,好像意思是单词前有个0位单词,只有0位,替换为#。然后是非匹配和非匹配或者首尾之间也有一个0位单词,所以也得替换。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值