/** * Replaces all occurances of the patternregex
with the String *replacement
* * @param hayStack */ private String replaceAllWithRegex(String hayStack) { String replacement = "replacement"; String regex = "nsw*:"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(hayStack); StringBuffer sb = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(sb, replacement); } matcher.appendTail(sb); return sb.toString(); }