Example usage for java.util.regex Matcher appendReplacement

List of usage examples for java.util.regex Matcher appendReplacement

Introduction

In this page you can find the example usage for java.util.regex Matcher appendReplacement.

Prototype

public Matcher appendReplacement(StringBuilder sb, String replacement) 

Source Link

Document

Implements a non-terminal append-and-replace step.

Usage

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("test");
    StringBuffer sb = new StringBuffer();

    String candidateString = "This is a test.";

    String replacement = "Test";
    Matcher matcher = p.matcher(candidateString);
    matcher.find();//from   w w  w .ja  va2s .c  o  m

    matcher.appendReplacement(sb, replacement);
}

From source file:Main.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("(another) (test)");
    StringBuffer sb = new StringBuffer();

    String candidateString = "This is another test.";

    String replacement = "$1 AAA $2";
    Matcher matcher = p.matcher(candidateString);
    matcher.find();//from  w  w w . ja v a  2 s  . c o m

    matcher.appendReplacement(sb, replacement);
    String msg = sb.toString();
    System.out.println(msg);
}

From source file:MainClass.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("(\\w+) (\\w+)");
    StringBuffer sb = new StringBuffer();

    String candidateString = "Jack Lee";

    String replacement = "$2, $1";
    Matcher matcher = p.matcher(candidateString);
    matcher.matches();//w w w  . j  a  va 2  s .  c o m

    matcher.appendReplacement(sb, replacement);

    System.out.println(sb.toString());
}

From source file:MatcherAppendReplacementGroupExample.java

public static void main(String args[]) {
    Pattern p = Pattern.compile("(James) (Bond)");
    StringBuffer sb = new StringBuffer();

    String candidateString = "My name is Bond. James Bond.";

    String replacement = "$1 Waldo $2";

    Matcher matcher = p.matcher(candidateString);
    matcher.find();//  w ww  .jav  a 2  s  .  co  m

    matcher.appendReplacement(sb, replacement);

    String msg = sb.toString();
    System.out.println(msg);
}

From source file:Main.java

public static void main(String[] args) {
    String str = "this is a test";

    StringBuffer sb = new StringBuffer();
    Matcher m = Pattern.compile("([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(str);
    while (m.find()) {
        m.appendReplacement(sb, m.group(1).toUpperCase() + m.group(2).toLowerCase());
    }/*  w w w  .  java 2 s.  co m*/
    System.out.println(m.appendTail(sb).toString());
}

From source file:MainClass.java

public static void main(String args[]) {
    String joke = "dog day daughter daut did do done date";
    String regEx = "d";

    Pattern doggone = Pattern.compile(regEx);
    Matcher m = doggone.matcher(joke);

    StringBuffer newJoke = new StringBuffer();
    while (m.find())
        m.appendReplacement(newJoke, "g");
    m.appendTail(newJoke);//from ww  w  .ja va2s  .  com
    System.out.println(newJoke.toString());
}

From source file:MainClass.java

public static void main(String[] av) {
    String joke = "dog " + "dogs";
    String regEx = "dog";

    Pattern doggone = Pattern.compile(regEx);
    Matcher m = doggone.matcher(joke);

    StringBuffer newJoke = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(newJoke, "goat");
    }/*from   ww w .  j  a va 2s  . co  m*/
    m.appendTail(newJoke);
    System.out.println(newJoke.toString());
}

From source file:RegexTest.java

public static void main(String[] args) {
    Pattern p = Pattern.compile(REGEX);
    Matcher m = p.matcher(INPUT); // get a matcher object
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        m.appendReplacement(sb, REPLACE);
    }//w ww.  j  av a 2 s.co  m
    m.appendTail(sb);
    System.out.println(sb.toString());
}

From source file:Main.java

public static void main(String[] args) {
    String input = " javaId=2 test test javaId=33432. javaId=100";

    Pattern p = Pattern.compile("(javaId=)(\\d+)");
    Matcher m = p.matcher(input);

    StringBuffer result = new StringBuffer();
    while (m.find()) {
        System.out.println("Masking: " + m.group(2));
        m.appendReplacement(result, m.group(1) + "***masked***");
    }/*from   w  ww.j  a v a  2  s. c  o  m*/
    m.appendTail(result);
    System.out.println(result);
}

From source file:TheReplacements.java

public static void main(String[] args) throws Exception {
    String s = TextFile.read("TheReplacements.java");
    // 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);//w w w  .j  av  a  2 s.c o m
    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);

}