57 lines
2.0 KiB
Java
57 lines
2.0 KiB
Java
// strings/TheReplacements.java
|
|
// (c)2016 MindView LLC: see Copyright.txt
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
// Visit http://mindviewinc.com/Books/OnJava/ 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 to
|
|
the regular expression matcher. Note that we
|
|
first extract the block of text by looking for
|
|
the special delimiters, then process the
|
|
extracted block. !*/
|
|
|
|
public class TheReplacements {
|
|
public static void
|
|
main(String[] args) throws Exception {
|
|
String s = Files.lines(
|
|
Paths.get("TheReplacements.java"))
|
|
.collect(Collectors.joining("\n"));
|
|
// Match the 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 one or more 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 to
|
|
the regular expression matcher. Note that we
|
|
first extract the block of text by looking for
|
|
the special delimiters, then process the
|
|
extracted block.
|
|
H(VOWEL1)rE's A blOck Of tExt tO UsE As InpUt tO
|
|
thE rEgUlAr ExprEssIOn mAtchEr. NOtE thAt wE
|
|
fIrst ExtrAct thE blOck Of tExt by lOOkIng fOr
|
|
thE spEcIAl dElImItErs, thEn prOcEss thE
|
|
ExtrActEd blOck.
|
|
*/
|