String字符串的replace函数和正则表达式
来源:互联网 发布:张孝祥的java就业教程 编辑:程序博客网 时间:2024/06/03 18:22
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
Stringjava.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
Stringjava.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; seejava.util.regex.Matcher.replaceFirst
. Usejava.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
Stringjava.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; seeMatcher.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位单词,所以也得替换。
- String字符串的replace函数和正则表达式
- Freemaker Replace函数的正则表达式运用
- [Javascript] String.replace 正则表达式及替换函数使用 $ 字符
- 【JavaScript】正则表达式--String.replace()变量替换的一点笔记
- 字符串的replace方法,结合正则表达式的使用
- 初识c#---字符串string和正则表达式
- [javascript-正则表达式]String.replace()用法详解
- String 对象中的 正则表达式方法 replace
- Java String.split()函数的用法和正则表达式
- java中String字符串的替换函数:replace与replaceAll的区别 和replaceFirst
- Mysql replace函数不能使用正则表达式的解决
- 强大的正则表达式:JavaScript中replace函数 替换变量
- replace应用的正则表达式
- string的字符串替换函数,类似于CString的Replace
- Java:String 字符串的替换函数 replace replaceAll replaceFirst
- javaScript里的正则表达式RegExp和replace
- 二:字符串和正则表达式_正则表达式函数
- JavaScript中replace函数,正则表达式替换replace
- 569. Median Employee Salary
- 第七届山东省ACM省赛 B Fibonacci
- 815
- Visual Studio 配置模板,快速定制初始页面内容
- ES6新特性之解构使用细节
- String字符串的replace函数和正则表达式
- 使用注解开发 MyBatis
- 第七届山东省ACM省赛 C Proxy
- zzuli 2134 维克兹的进制转换
- 宏晶STC单片机使用STC-ISP串口烧录失败的解决方法及实例汇总 (Ver0.99.16)
- home-ids-with-snort-and-snorby
- 伪元素和伪类的区别
- matlab下生成的矩阵存入xml文件中
- poj3335 Rotating Scoreboard【半平面交】