import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { 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(); matcher.appendReplacement(sb, replacement); } }