正确的方法是使用解析器(处理\\,
\,
,
),但使用简单的正则表达式可以工作;
jshell> "a,b".split("(?!\\\\),")
$2 ==> String[2] { "a", "b" }
如何测试不起作用的东西;
jshell> "a,b".split("[^\\\\],")
$1 ==> String[2] { "", "b" }
和
jshell> "a,b".split("*^\\,*")
| java.util.regex.PatternSyntaxException thrown: Dangling meta character '*' near index 0
*^\,*
^
| at Pattern.error (Pattern.java:1997)
| at Pattern.sequence (Pattern.java:2172)
| at Pattern.expr (Pattern.java:2038)
| at Pattern.compile (Pattern.java:1760)
| at Pattern.<init> (Pattern.java:1409)
| at Pattern.compile (Pattern.java:1065)
| at String.split (String.java:2307)
| at String.split (String.java:2354)
| at (#6:1)