你可以使用正则表达式
(?:\\.|[^;\\]++)*
匹配未转义分号之间的所有文本:
List matchList = new ArrayList();
try {
Pattern regex = Pattern.compile("(?:\\\\.|[^;\\\\]++)*");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
matchList.add(regexMatcher.group());
}
说明:
(?: # Match either...
\\. # any escaped character
| # or...
[^;\\]++ # any character(s) except semicolon or backslash; possessive match
)* # Repeat any number of times.
由于嵌套量词,占有性匹配()对于避免灾难性回溯非常重要.