Search code examples
javascriptlet

Java scriptlet to remove duplicate in string separated by | (delimiter)


I have a String that looks like "(doc1| doc1| provid| geestt| stable)". It represents an array and I want to remove any duplicates from it so that doc1 appears only once. How can I do this?


Solution

  •     String s = "(doc1|doc1|provid|geestt|provid|stable)";
        s = s.replaceAll("\\b(\\w+)\\|(?=.*\\b\\1\\b)", "");
        System.out.println(s);
        // (doc1|geestt|provid|stable)
    

    This uses \\w for word char; probably you need [^|)] that is: not one of the separator chars. The 0-char wide \\b is for detecting word boundaries, that fits.

    This pattern uses a look-ahead (?= ... ) containing \\1 the 1st matched () group: the word.

    P.S. Set<String> seems to be a more appropiate data structure.