Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a string-capable Guava Splitter

I would like to create a Guava Splitter for Java that can handles Java strings as one block. For instance, I would like the following assertion to be true:

@Test
public void testSplitter() {
  String toSplit = "a,b,\"c,d\\\"\",e";
  List<String> expected = ImmutableList.of("a", "b", "c,d\"","e");

  Splitter splitter = Splitter.onPattern(...);
  List<String> actual = ImmutableList.copyOf(splitter.split(toSplit));

  assertEquals(expected, actual);
}

I can write the regex to find all the elements and don't consider the ',' but I can't find the regex that would act as a separator to be used with a Splitter.

If it's impossible, please just say so, then I'll build the list from the findAll regex.

like image 886
Olivier Grégoire Avatar asked Dec 10 '22 09:12

Olivier Grégoire


1 Answers

This seems like something you should use a CSV library such as opencsv for. Separating values and handling cases like quoted blocks are what they're all about.

like image 109
ColinD Avatar answered Dec 13 '22 22:12

ColinD