1

I am trying to split a string such as String s = "do not split this \"split this\"";

String[] split = s.split("(?<=\\s)| (?=\") | ((?=[^A-Za-z0-9])|(?<=[^A-Za-z0-9]));

will give me ["do", " ", "not", " ", "split", "this", " ", "split this"];

I would like to keep all words, white spaces as well, but ignore anything inside double quotes~

user3413646
  • 167
  • 2
  • 11
  • You can match that using `String regex="\"([^\"]*)\"|\\S+|\\s+";` and if there is Group 1 match, get the Group 1 value, else, get the whole match value. – Wiktor Stribiżew Apr 08 '20 at 15:40
  • i want to keep words and white spaces, but anything that are in between quotes, i want to keep as whole – user3413646 Apr 08 '20 at 15:51

1 Answers1

1

just a guess:

String s = "do not split this \"split this\"";
String[] split = s.split( "(?<!\".{0,255}) | (?!.*\".*)" );  // do, not, split, this, "split this"

don't split at the blank, if the blank is surrounded by quotes
split at the blank when the 255 characters to the left and all characters to the right of the blank are no quotes

Kaplan
  • 2,572
  • 13
  • 14