java regex to find unique digits

I have a string of some length (may vary) with digits, for example:

String strInput = "0002334556";

I need to get all unique digits from strInput and allocate them to a different string like this:

String uniques = "023456";

Currently stuck with what regex I should use to parse through strInput. Non-regex solutions are also welcome. This is not a real life task, but an academic one which favors regex.

>Solution :

Try this.

public static void main(String[] args) {
    String strInput = "00023345560303";

    String uniques = Arrays.stream(strInput.split("(?<=.)"))
        .distinct().collect(Collectors.joining());

    System.out.println(uniques);
}

output:

023456

Leave a Reply