我没有试过扩展标记生成器,但我在这里用正则表达式一个不错的(我认为)解决方案:
\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相同的结果。再次感谢你:) –