目标串: "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<String> list = new ArrayList<>(); while (matcher.find()) { list.add(matcher.group()); } for (String s : list){ System.out.println(s); } }