Regular expression replacement - Java Regular Expressions

Java examples for Regular Expressions:Replace

Description

Regular expression replacement

Demo Code

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

public class Main {
  public static void main(String[] argv) {
    String regex = "test";
    String replace = "t";

    Pattern pattern = Pattern.compile(regex);

    Matcher matcher = pattern.matcher("");
    matcher.reset("s");
    System.out.println(matcher.replaceFirst(replace));
    System.out.println(matcher.replaceAll(replace));
  }/*  w  w w.  j a va  2 s.  co  m*/
}

Result


Related Tutorials