使用a
Matcher来标识要保留的部分,而不是要拆分的部分:
String s = "hello^world'this*has two tokens'";Pattern pattern = Pattern.compile("([a-zA-Z0-9]+|'[^']*')+");Matcher matcher = pattern.matcher(s);while (matcher.find()) { System.out.println(matcher.group(0));}看到它在线上工作:ideone



