Java Utililty Methods Regex String Replace Last

List of utility methods to do Regex String Replace Last

Description

The list of methods to do Regex String Replace Last are organized into topic(s).

Method

StringreplaceLast(String foo, String regex, String replacement)
Sostituisce nella stringa foo l'ultima occorrenza della regex con replacement (es.
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(foo);
int start = -1;
int end = -1;
while (m.find()) {
    start = m.start();
    end = m.end();
if (start < 0) {
    return foo;
return foo.substring(0, start) + replacement + foo.substring(end);
StringreplaceLast(String input, String regex, String replacement)
replace Last
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
if (!matcher.find()) {
    return input;
int lastMatchStart;
do {
    lastMatchStart = matcher.start();
...
StringreplaceLast(String string, String regex, String replacement)
Works like String.replaceFirst, but replaces the last instance instead.
if (regex == null) {
    return string;
if (string == null) {
    return null;
if (regex.length() > string.length()) {
    return string;
...