我正在尝试分割一个字符串,例如 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"];
我想保留所有单词,空格,但忽略双引号内的任何内容〜
最佳答案
只是一个猜测:
String s = "do not split this \"split this\"";
String[] split = s.split( "(?<!\".{0,255}) | (?!.*\".*)" ); // do, not, split, this, "split this"
如果空格被引号包围,则不要在空格处拆分
当左侧 255 个字符和空白右侧的所有字符都没有引号时,在空白处拆分
关于Java用正则表达式分割字符串,双引号内的任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61104216/