Regular expression append/replace - Java Regular Expressions

Java examples for Regular Expressions:Replace

Description

Regular expression append/replace

Demo Code

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
  public static void main(String[] argv) {
    String input = "Thanks, thanks very much";
    String regex = "([Tt])hanks";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);
    StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
      if (matcher.group(1).equals("T")) {
        matcher.appendReplacement(sb, "Thank you");
      } else {// w w w  .ja  v  a2s  .  c o m
        matcher.appendReplacement(sb, "thank you");
      }
    }
    matcher.appendTail(sb);
    System.out.println(sb.toString());
    sb.setLength(0);
    matcher.reset();
    String replacement = "$1hank you";
    while (matcher.find()) {
      matcher.appendReplacement(sb, replacement);
    }
    matcher.appendTail(sb);
    System.out.println(sb.toString());
    System.out.println(matcher.replaceAll(replacement));
    System.out.println(input.replaceAll(regex, replacement));

  }
}

Result


Related Tutorials