6
votes

Playing with Guava I can either set the split to be a single character, a string or a regular expression.

What I want is to split on multiple inputs without having to resort to using regular expressions as I want to built up the separators using method calls.

What I'm trying to do is to get it to let me do something like:

Splitter.on(',')
    .on('.')
    .on('|')
    .on("BREAK")
    .splitToList(test);

So for the input "a,b,c.d|e BREAK f" would produce a list containing a/b/c/d/e/f.

This is done within a class I'm writing so maybe construct a regular expression from the inputs when the call is made to finally process the data and use this as the one and only .on() call?

Thanks.

1
I'm not sure this is doable generally -- what if you did .on('B').on("BREAK"), how would that even work? -- but regexes are certainly the only way to approximate this with Guava's Splitter. - Louis Wasserman
.on is static so doesn't allow it and without looking at the code it seems that it stores a single item for the split, i.e. one of char, string, regex - Neil Walker

1 Answers

7
votes

As @Louis Wasserman pointed out, you want to use a regular expression here. You can either use Splitter.on(Pattern) or Splitter.onPattern(String) to accomplish this, and construct a regular expression to match the separators, like ([,.|]|BREAK).

Here is my method with a test. This test fails for the CharMatcher approach, and succeeds with the regular expression.

public class SplitterTest {
    public String splitAndRejoin(String pre) {
        return Joiner.on("/").join(
            Splitter
                .onPattern("([,.|]|BREAK)")
                .trimResults()
                .omitEmptyStrings()
                .split(pre));
    }

    @Test
    public void test1() {
        assertEquals("a/b/c/d/e/f", splitAndRejoin("a,b,c.d|e BREAK f"));
        assertEquals("A/B/C/D/E/F", splitAndRejoin("A,B,C.D|E BREAK F"));
    }
}