String input = "abcd"; Pattern p = Pattern.compile("regexp"); String output = p.matcher(input).replaceAll("replacement");