英文:
Java split string with regex, anything inside Double Quotes
问题
我正在尝试分割一个字符串,类似于 String s = "do not split this \"split this\"";
String[] split = s.split("(?<=\\s)| (?=\") | ((?=[^A-Za-z0-9])|(?<=[^A-Za-z0-9]));
将会得到 ["do", " ", "not", " ", "split", "this", " ", "split this"];
我想保留所有单词、空白,但忽略双引号内的任何内容~
英文:
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
得分: 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*<br />
split at the blank when the 255 characters to the left *and* all characters to the right of the blank are no quotes
英文:
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<br />
split at the blank when the 255 characters to the left and all characters to the right of the blank are no quotes
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论