你可以使用正則表達式
(?:\\.|[^;\\]++)*
匹配未轉義分號之間的所有文本:
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.
由于嵌套量詞,占有性匹配()對于避免災難性回溯非常重要.