Java split string with regex, anything inside Double Quotes [duplicate]

こ雲淡風輕ζ 提交于 2021-02-11 13:58:56

问题


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~


回答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



来源:https://stackoverflow.com/questions/61104216/java-split-string-with-regex-anything-inside-double-quotes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!