Java examples for Regular Expressions:Replace
Regular expression replacement
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*/ }