List of utility methods to do Regex String Replace
String | replace(String source, String search, String replace) replace source = (source == null) ? "" : source; if (search == null) { return source; replace = replace == null ? "" : replace; return Pattern.compile(Pattern.quote(search)).matcher(source).replaceAll(replace); |
String | replace(String source, String search, String replacement) replace System.out.println("replace( " + source + ", " + search + ", " + replacement + ")"); String result = null; search = "\\Q" + prepareNonRegularExpression(search) + "\\E"; replacement = prepareNonRegularExpressionReplacement(replacement); Pattern pattern = Pattern.compile(search, Pattern.CASE_INSENSITIVE); try { Matcher matcher = pattern.matcher(source); result = matcher.replaceAll(replacement); ... |
String | replace(String source, String[] patterns, String[] replacements) Search source for instances of patterns from patterns[]. if (source == null) return null; StringBuffer buf = new StringBuffer(); for (int i = 0; i < patterns.length; ++i) { if (i != 0) buf.append("|"); buf.append("(").append(patterns[i]).append(")"); String regexString = buf.toString(); Pattern regex = Pattern.compile(regexString); Matcher m = regex.matcher(source); if (m.groupCount() != replacements.length) throw new IllegalArgumentException("Mismatch between pattern and replacements array"); StringBuffer result = null; int idx = 0; while (m.find()) { if (result == null) result = new StringBuffer(source.length() + 32); result.append(source.substring(idx, m.start())); for (int i = 0; i < replacements.length; ++i) { if (m.group(i + 1) != null) { result.append(replacements[i]); idx = m.end(); break; if (result == null) return source; result.append(source.substring(idx)); return result.toString(); |
String | replace(String str, String key, String value) Replace all {key}} in the given string with value . return str.replaceAll(TAG_OPEN.matcher(TAG_CLOSE.matcher(key).replaceAll("\\\\{")).replaceAll("\\\\}"), value); |
String | replace(String str, String replacement, Function replace return replace(str, replacement, func, 0);
|
String | replace(String string, Pattern pattern, String replacement) replace Matcher m = pattern.matcher(string);
return m.replaceAll(replacement);
|
String | replace(String string, Pattern[] patterns, String[] replacements) Replaces all occurrences of several regular expressions in order. for (int i = 0, length = patterns.length; i < length; i++) string = patterns[i].matcher(string).replaceAll(replacements[i]); return string; |
String | replace(String string, Properties table) Equivalent to replace(string, table, false) .
return replace(string, table, false);
|
String | replace(String string, String pattern, String replacement, boolean literal) Replaces all occurrences of the given regular expression in the given string with the given replacement. return replace(string, pattern, replacement, literal, false);
|
String | replace(String text, Map replace for (Entry<String, String> var : vars.entrySet()) text = text.replaceAll(Pattern.quote(prefix + var.getKey()), var.getValue()); return text; |