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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With