获取扫描器使用的当前分隔符 Class

Get Current Delimiter Used by Scanner Class

当分隔符是正则表达式时,是否可以获取扫描程序正在使用的 current 分隔符?例如我有这个代码:

        String dictionary = "computer: A computer is an electronic machine that can store\n"
                          + "          and deal with large amounts of information.\n"
                          + "computer-aided: done or improved by computer\n"; 
        Scanner src = new Scanner(dictionary);
        String delimiterRegex = "^(.+?:)"; // <-- Matches a new term
        Pattern delimiterPattern = Pattern.compile(delimiterRegex, Pattern.MULTILINE);
        src.useDelimiter(delimiterPattern);
        String definition = "";
        String term = "";

        while(src.hasNext())
        {
            definition = src.next();
            term = ???????; // <--- The term is the current delimiter match
        }

这是获取所有定义的一种非常简单的方法,要是我也能获取术语就好了。

没有办法使用 Scanner API.

但是,如果您查看 Scanner 的源代码,您会看到有一个用于匹配分隔符的私有 Matcher 对象。如果你愿意打破 Scanner 抽象(通过讨厌的反射),你可以从匹配器中提取你需要的信息......如果你在适当的时间检查它。

如果您打算尝试这个,我的建议是使用 Scanner 源代码来创建您自己的自定义扫描器 class。这将使您的代码不受标准 Scanner class.

的实现更改的影响

确保您从 OpenJDK 获取源代码,并满足文件上 "GPLv2" 许可证的要求。


对于它的价值,我同意中Sweeper所说的。对于这个问题,捕获一个token作为分隔符是错误的做法。

将 "term" 视为第一个 class 标记。我会使用 Scanner.next(Pattern) 来解析令牌,其中 Pattern 匹配 "term".

这是一个XY problem

与其尝试获取扫描器的 匹配的 定界符(这是一个实现细节),不如重写定界符正则表达式,以便 next returns 什么你要。

例如:

// this matches both the zero-width string before the term, and the zero-width string after the colon
String delimiterRegex = "^(?=.+?:)|(?<=:)";
Pattern delimiterPattern = Pattern.compile(delimiterRegex, Pattern.MULTILINE);
src.useDelimiter(delimiterPattern);
String definition = "";
String term = "";

while(src.hasNext())
{
    term = src.next(); // read the term first!
    definition = src.next();
}

或者,只需使用一个正则表达式即可。我能够想出:

Pattern p = Pattern.compile("([^:\r\n]+?:)([\s\S]+?)(?=^[^:\r\n]+?:|\z)", Pattern.MULTILINE);
Matcher m = p.matcher(dictionary);
while (m.find()) {
    String term = m.group(1);
    String definition = m.group(2);
}