Java 如果不是在引号之间,则在空格处拆分
我试过这个:Java 如果不是在引号之间,则在空格处拆分,java,regex,Java,Regex,我试过这个: +|(?!(\"[^"]*\")) 但它不起作用。 我还能做些什么来让它工作呢? 顺便说一句,我正在使用java的string.split() var str="Hi there" var splitOutput=str.split(" "); //splitOutput[0]=Hi //splitOutput[1]=there 对不起,我误解了你的问题 从巴特的解释中添加这个(=([^“]*“[^”]*”*[^“]*”*[^”]*$)或[]+(?=([^“]*”[^“]*
+|(?!(\"[^"]*\"))
但它不起作用。
我还能做些什么来让它工作呢?
顺便说一句,我正在使用java的string.split()
var str="Hi there"
var splitOutput=str.split(" ");
//splitOutput[0]=Hi
//splitOutput[1]=there
对不起,我误解了你的问题
从巴特的解释中添加这个(=([^“]*“[^”]*”*[^“]*”*[^”]*$)或
[]+(?=([^“]*”[^“]*”*[^”]*$)
这就是你要找的吗
input.split("(?<!\") (?!\")")
input.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
文本=a“b c d”e“f g”h
A.
“b c d”
E
“f g”
H
我猜OP还想看看引号中的空格是否是这样的:“text more text”
(其中两个空格)它不起作用,我想要的结果是:“hi there\“hi there\”->[“hi”,“there”,“hi there”]我已经回答了(几乎)相同的问题:它适用于C,但解决方案应该适用于java。