@SuppressWarnings("JdkObsolete")
    public static Object[] replaceAllWithRegex(
            String source, String regex, String replacement, boolean caseSensitive) {
        java.util.regex.Pattern pattern = caseSensitive ?
                java.util.regex.Pattern.compile(regex) :
                java.util.regex.Pattern.compile(regex, java.util.regex.Pattern.CASE_INSENSITIVE);
        final String replacementQuoted = Matcher.quoteReplacement(replacement);
        Matcher matcher = pattern.matcher(source);
        int totalReplaced = 0;
        // Can be replaced with StringBuilder for Java 9+
        StringBuffer result = new StringBuffer(); // NOSONAR Matcher#appendReplacement needs a StringBuffer
        while (matcher.find()) {
            matcher.appendReplacement(result, replacementQuoted);
            totalReplaced++;
        }
        matcher.appendTail(result);

        return new Object[]{
                result.toString(),
                totalReplaced
        };
    }
@Test
    public void testReplaceAllWithRegex() {
        assertArrayEquals(new Object[]{"toto", 0},
                JOrphanUtils.replaceAllWithRegex("toto", "ti", "ta", true));
        assertArrayEquals(new Object[]{"toto", 0},
                JOrphanUtils.replaceAllWithRegex("toto", "TO", "TI", true));
        assertArrayEquals(new Object[]{"TITI", 2},
                JOrphanUtils.replaceAllWithRegex("toto", "TO", "TI", false));
        assertArrayEquals(new Object[]{"TITI", 2},
                JOrphanUtils.replaceAllWithRegex("toto", "to", "TI", true));
        assertArrayEquals(new Object[]{"TITIti", 2},
                JOrphanUtils.replaceAllWithRegex("tototi", "to", "TI", true));
        assertArrayEquals(new Object[]{"TOTIti", 1},
                JOrphanUtils.replaceAllWithRegex("TOtoti", "to", "TI", true));
        assertArrayEquals(new Object[]{"TOTI", 1},
                JOrphanUtils.replaceAllWithRegex("TOtoti", "to.*", "TI", true));
        assertArrayEquals(new Object[]{"TOTI", 1},
                JOrphanUtils.replaceAllWithRegex("TOtoti", "to.*ti", "TI", true));
        assertArrayEquals(new Object[]{"TOTITITITIaTITITIti", 7},
                JOrphanUtils.replaceAllWithRegex("TO1232a123ti", "[0-9]", "TI", true));
        assertArrayEquals(new Object[]{"TOTIaTIti", 2},
                JOrphanUtils.replaceAllWithRegex("TO1232a123ti", "[0-9]+", "TI", true));

        assertArrayEquals(new Object[]{"TO${var}2a${var}ti", 2},
                JOrphanUtils.replaceAllWithRegex("TO1232a123ti", "123", "${var}", true));

        assertArrayEquals(new Object[]{"TO${var}2a${var}ti${var2}", 2},
                JOrphanUtils.replaceAllWithRegex("TO1232a123ti${var2}", "123", "${var}", true));
    }