0

Is there a short way (maybe str.split variation) for parsing a string by white spaces while bypassing when it is between quotes?

example: this should be split "this is not" and so on

result:

[0] - this; [1] - should; [2] - be; [3] - split; [4] - "this is not"; 
[5] - and; [6] - so; [7] - on;

Thanks

Mark
  • 1,350
  • 1
  • 11
  • 20

1 Answers1

2

Try this:

String words = input.split(" +(?=([^\"]*\"[^\"]*\")*[^\"]*$)");

It's saying split only if there's an even number of quotes to the right. It doesn't cater for escaped quotes, but that's not hard to do - just becomes very hard to read.

Bohemian
  • 365,064
  • 84
  • 522
  • 658
  • I'd say matching with Matcher is more flexible than split for this case... – nhahtdh Apr 25 '13 at 17:47
  • @nhahtdh what do you mean? Why is flexibility relevant? The task is to split. Why would you use several lines when one call will do it? – Bohemian Apr 25 '13 at 22:09
  • For example - the case when there are odd number of `"`, or the case of `"abc"next to quote`. It is easier to customize the regex than `split`. – nhahtdh Apr 26 '13 at 01:01