Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{"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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading