Search code examples
javasplitguavasplitter

Pattern for Guava Splitter


I need to split String by comma or dot or backslach :

Pattern stringPattern = Pattern.compile("\\s+|,|\\\\|");
Splitter.on(stringPattern).omitEmptyStrings().split(description));

but this pattern don't work , what is wrong ?


Solution

  • The correct regex for comma or dot or backslash is [.,\\], so in Java that's

    Pattern.compile("[.,\\\\]")
    

    I do like Olivier's suggestion of CharMatcher though.