正则表达式之以空格分割字符串但忽略引号中的内容
目标串: "aaa bbb "cc dd" eee fff"
期望结果:
aaa
bbb
"cc dd"
eee
fff
具体实现:
private static final Pattern PATTERN = Pattern.compile("\"([^\"]*?)\"|(\\S+)");
public static void main(String[] args) {
String str = "aaa bbb \"cc dd\" eee fff";
Matcher matcher = PATTERN.matcher(str);
List list = new ArrayList<>();
while (matcher.find()) {
list.add(matcher.group());
}
for (String s : list){
System.out.println(s);
}
}