Search code examples
javaregexsplit

How to split a string between commas, one or more spaces and between digits and letters but not between dots with regex in java?


As the title says I like to split a String between commas, one or more spaces and between digits and letters but not between dots with regex in java?

So for eample if I have the following String

"ab,cd76253  eruizgbe 19.05.1976, eribfuer243 fg"

I want to have an Array like this:

{"ab","cd","76253","eruizgbe","19.05.1976","eribfuer","243","fg"}

I have the following:

"ab,cd76253  eruizgbe 19.05.1976, eribfuer243 fg".split("[\\s,]+|(?<=\\D)(?=\\d)|(? 
<=\\d)(?=\\D)");

But this also splits the the date between digits and dots. How can I prevent to split between the digits and dots?


Solution

  • You can use [^\d.] instead of \D to exclude digits and dots.

    Also I've added \s, to negation, to exclude double split between space (or comma) and digits: [\s,]+|(?<=[^\d\s.,])(?=\d)|(?<=\d)(?=[^\d\s.,])

    for (String s : "ab,cd76253  eruizgbe 19.05.1976, eribfuer243 fg"
      .split("[\\s,]+|(?<=[^\\d.\\s,])(?=\\d)|(?<=\\d)(?=[^\\d.\\s,])")){
                System.out.println(s);
            }
    

    Output:

    ab
    cd
    76253
    eruizgbe
    19.05.1976
    eribfuer
    243
    fg
    

    Demo at regex101