关于正则表达式。。blablabla
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
package String01; import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match; import java.util.regex.Matcher; import java.util.regex.Pattern; public class TheReplacement { public static void main(String[] args) { String s=new String(new StringBuilder().append("/*!Here's a block of text as use as input to\n") .append("the regular expression matcher.Note that we'll\n") .append("first extract the block of text by looking for\n") .append("the special delimiters,then process the\n") .append("extracted block. !*/").toString()); Matcher minput =Pattern.compile("/\\*!(.*)!\\*/",Pattern.DOTALL).matcher(s); if(minput.find()){ s=minput.group(1); } //s=s.replaceAll(" {2,}"," ");//匹配至少两个空格,并且替换为一个空格! //s=s.replaceFirst("[aeiou]","(The first Vowel)"); StringBuffer sbuf=new StringBuffer(); Pattern p=Pattern.compile("[aeiou]"); Matcher m=p.matcher(s); while (m.find()){ m.appendReplacement(sbuf,m.group().toUpperCase());//appendReplacement方法使用在其思想模拟了String的append方法 //可以不断的qppend处理而不是一次切分成若干reg子式处理, //并且appendrReplacement方法可以向一个StringBuffer不断写入 } System.out.println(sbuf.toString()); //注意此处输出的sbuf是最后一个find()之前的所有字符,但是find()之后的字符串没有放入sbuf //因此要使用appendtail函数来放入 m.appendTail(sbuf); System.out.println(sbuf.toString());//可以看到最后的ck完整了 } }