Neil Walker
Neil Walker

Reputation: 6848

Guava Splitter with multiple split parameters

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.

Upvotes: 6

Views: 3903

Answers (1)

Ray
Ray

Reputation: 4869

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"));
    }
}

Upvotes: 7

Related Questions