Question

I gave it my best shot and this is what I have so far.

String input = "hey,what up man:1033:yes,okay that makes sense     ,not:       okay:1111,     aaaaa,bbbbb";
String input2 = input.replaceAll("(.*)\\s([^:,])+\\s(.*)", "\1\2\3");
String arguments[] = input2.split("[:,]");

Expected output:
arguments[0] ="hey";
arguments[1]="what up man";
arguments[2]="1033";
arguments[3]="yes";
arguments[4]="okay that makes sense";
arguments[5]="not";
arguments[6]="okay";
arguments[7]="1111";
arguments[8]="aaaaa";
arguments[9]="bbbbb";


Pretty much I need to be able to get all 10 arguments and strip the Whitespace right before each of the delimiters ; and , and all the Whitespace right after each of the delimiters.

Would be cool if it's possible to do this with just split alone without replaceAll, I assume with some very expert regex skills anything is possible in a one liner.

Was it helpful?

Solution

You can include whitespaces surrounding your delimiters by adding \\s*. Try this way

String arguments[] = input.split("\\s*[:,]\\s*");

DEMO:

String input = "hey,what up man:1033:yes,okay that makes sense     ,not:       okay:1111,     aaaaa,bbbbb";
String arguments[] = input.split("\\s*[:,]\\s*");
for (int i=0; i<arguments.length; i++){
    System.out.println(i+")"+arguments[i]);
}

output:

0)hey
1)what up man
2)1033
3)yes
4)okay that makes sense
5)not
6)okay
7)1111
8)aaaaa
9)bbbbb
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top