【发布时间】:2017-05-01 17:31:23
【问题描述】:
基本上,我需要得到a b c(单独)
从一行开始(每个“”之间有任意数量的空格
"a" "b" "c"
是否可以使用 string.split 来做到这一点?
我尝试了从split(".*?\".*?") 到("\\s*\"\\s*") 的所有方法。
后者有效,但它将数据拆分为数组的每个其他索引(1、3、5),其他索引为空“”
编辑:
我希望这适用于任何数量/变化的字符,而不仅仅是 a、b 和 c。 (例如:"apple" "pie" "dog boy")
为我的具体问题找到了解决方案(可能不是最有效的):
Scanner abc = new Scanner(System.in);
for loop
{
input = abc.nextLine();
Scanner in= new Scanner(input).useDelimiter("\\s*\"\\s*");
assign to appropriate index in array using in.next();
in.next(); to avoid the spaces
}
【问题讨论】:
-
您还希望输出为
a b c而不是"a" "b" "c"正确吗? -
@brso05 我已经尝试了从 split(".*?\".*?") 到 ("\\s*\"\\s*") 的所有方法。后者有效,但它将数据拆分为数组的每个其他索引(1、3、5),其他索引为空“”。
-
看起来你想replace all " 使用空字符串
-
@brso05 正确,没有引号。
-
如果您在
\"([a-z])\"上进行模式匹配怎么办?