// strings/TheReplacements.java// (c)2017 MindView LLC: see Copyright.txt// We make no guarantees that this code is fit for any purpose.// Visit http://OnJava8.com for more book information.import java.util.regex.*;import java.nio.file.*;import java.util.stream.*;/*! Here's a block of text to use as input tothe regular expression matcher. Note that wefirst extract the block of text by looking forthe special delimiters, then process theextracted block. !*/public class TheReplacements {public static voidmain(String[] args) throws Exception {String s = Files.lines(Paths.get("TheReplacements.java")).collect(Collectors.joining("\n"));// Match specially commented block of text above:Matcher mInput = Pattern.compile("/\\*!(.*)!\\*/", Pattern.DOTALL).matcher(s);if(mInput.find())s = mInput.group(1); // Captured by parentheses// Replace two or more spaces with a single space:s = s.replaceAll(" {2,}", " ");// Replace 1+ spaces at the beginning of each// line with no spaces. Must enable MULTILINE mode:s = s.replaceAll("(?m)^ +", "");System.out.println(s);s = s.replaceFirst("[aeiou]", "(VOWEL1)");StringBuffer sbuf = new StringBuffer();Pattern p = Pattern.compile("[aeiou]");Matcher m = p.matcher(s);// Process the find information as you// perform the replacements:while(m.find())m.appendReplacement(sbuf, m.group().toUpperCase());// Put in the remainder of the text:m.appendTail(sbuf);System.out.println(sbuf);}}/* Output:Here's a block of text to use as input tothe regular expression matcher. Note that wefirst extract the block of text by looking forthe special delimiters, then process theextracted block.H(VOWEL1)rE's A blOck Of tExt tO UsE As InpUt tOthE rEgUlAr ExprEssIOn mAtchEr. NOtE thAt wEfIrst ExtrAct thE blOck Of tExt by lOOkIng fOrthE spEcIAl dElImItErs, thEn prOcEss thEExtrActEd blOck.*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。