我尝试了这个:
+|(?!(\"[^"]*\"))
但它没有起作用。 我还能做些什么让它起作用吗? 顺便提一下,我正在使用Java的string.split()。
试试这个:
[ ]+(?=([^"]*"[^"]*")*[^"]*$)
该功能将仅在空格后面跟随零个或偶数个引号(一直到字符串的结尾)时才拆分。
以下演示:
public class Main {
public static void main(String[] args) {
String text = "a \"b c d\" e \"f g\" h";
System.out.println("text = " + text + "\n");
for(String t : text.split("[ ]+(?=([^\"]*\"[^\"]*\")*[^\"]*$)")) {
System.out.println(t);
}
}
}
将产生以下输出:
text = a "b c d" e "f g" h
a "b c d" e "f g" h
这是你要找的吗?
input.split("(?<!\") (?!\")")
这个可以工作吗?
var str="Hi there"
var splitOutput=str.split(" ");
//splitOutput[0]=Hi
//splitOutput[1]=there
抱歉,我误解了你的问题
从 Bart 的解释中添加这个 \s(?=([^"]*"[^"]*")*[^"]*$)
或者 [ ]+(?=([^"]*"[^"]*")*[^"]*$)