2

Suppose I have a string

123 --o 45 xyz 67 "abc def" " ghi jkl m" " " "" xy z

which I need to parse into an array of strings

["123", "--o", "45", "xyz", "67", "abc def", " ghi jkl m", " ", "", "xy", "z"]

My straight approach to split a string by spaces (split("\\s+")) isn't suitable because it separates a string by spaces and doesn't consider double quotes.

But I also need to separate elements which is quoted (" ghi jkl m", "abc def", and " ").

How can I modify my regular expression in method split to achieve my goal?

UPD

We also should consider the spaces.

=> [a, "s ", abc, "", "ad"sdsd"sdsd"]

"ad"sdsd"sdsd" is a sinle element.

2 Answers 2

1

Split your input according to the below regex which uses a positive lookahead assertion.

String text = "123 --o 45 xyz 67 \"abc def\" \" ghi jkl m\" \" \" \"\" xy z";
String parts[] = text.split("\\s+(?=(?:\"[^\"]*\"|[^\"])*$)");
System.out.println(Arrays.toString(parts));

Output:

[123, --o, 45, xyz, 67, "abc def", " ghi jkl m", " ", "", xy, z]
Sign up to request clarification or add additional context in comments.

3 Comments

i answered these type of question n number of times. So i make this answer as CW.
But if I have an element like a"b"c? what about that case? I need to parsed as a string "a"b"c". We need to take into account the spaces.
yep, i would return a"b"c. All the above are strings.. Arrays.toString function convert the array elements to strings.
0

or use this pattern to capture what you want

("[^"]+"|\S+)  

Demo

2 Comments

It doesn't split string like this "abc def g"h"i 100" abc 100. should be ["abc def g"h"i 100", "abc", "100"] but I get ["abc def g"h"i", "100", "abc", "100"].
would you split "abc def g " h " i 100" abc 100. ? into ["abc def g " h " i 100", "abc", "100"] ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.