我沒有試過擴展標記生成器,但我在這裏用正則表達式一個不錯的(我認爲)解決方案:
\w+|\(\([\w\s]*\)\)
和從REG通過匹配組分割字符串的方法ex返回一個數組。代碼示例:
class Regex_ComandLine {
public static void main(String[] args) {
String input = "term1 term2 term3 ((term4 term5)) term6"; //your input
String[] parsedInput = splitByMatchedGroups(input, "\\w+|\\(\\([\\w\\s]*\\)\\)");
for (String arg : parsedInput) {
System.out.println(arg);
}
}
static String[] splitByMatchedGroups(String string,
String patternString) {
List<String> matchList = new ArrayList<>();
Matcher regexMatcher = Pattern.compile(patternString).matcher(string);
while (regexMatcher.find()) {
matchList.add(regexMatcher.group());
}
return matchList.toArray(new String[0]);
}
}
輸出:
term1
term2
term3
((term4 term5))
term6
希望這有助於你。
請注意,下面的代碼與通常split()
:
String[] parsedInput = input.split("\\w+|\\(\\([\\w\\s]*\\)\\)");
將返回什麼,或者你想怎麼一回事,因爲它只檢查分隔符不算什麼。
謝謝。我想我會用這種方法最簡單,我可以獲得與擴展WhitespaceTokenizer相同的結果。再次感謝你:) –