Java Format Number with Blanks

I have a number like this:

1234567890123

and want to format it for example like this

12 34567 89012 3

or maybe like this

1-234 567-890 1 23

How can I achive that? I tried this

DecimalFormat decimalFormat = new DecimalFormat("00 00000 00000 0");
System.out.println(decimalFormat.format(1234567890123L));

But this doesn’t work.

I need a way to specify a pattern that can contain any separator character.

Answer

Thanks to @McPringle I finally implemented this solution:

public class MaskFormatter {

    private char[] separators;

    public String formatNumber(Number number, String pattern) {
        if (number == null) {
            return "";
        } else {
            separators = pattern.replace("0", "").toCharArray();

            String string = number.toString();
            String regex = createRegex(pattern);
            String replacement = createReplacement(pattern);

            return string.replaceAll(regex, replacement);
        }
    }

    private String createRegex(String pattern) {
        String[] parts = pattern.split("[" + createPatternFromSeparators() + "]");

        StringBuilder sb = new StringBuilder();
        for (String part : parts) {
            sb.append("(\d{").append(part.length()).append("})");
        }
        return sb.toString();
    }

    private String createReplacement(String pattern) {
        String[] parts = pattern.split("[" + createPatternFromSeparators() + "]");

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < parts.length; i++) {
            if (i > 0) {
                char separator = separators[i - 1];
                sb.append(separator);
            }
            sb.append("$").append(i + 1);
        }
        return sb.toString();
    }

    private String createPatternFromSeparators() {
        Set<String> set = new HashSet<>();
        for (char separator : separators) {
            set.add(String.valueOf(separator));
        }
        return String.join("", set);
    }
}