1

我正在制作一个 java IRC lib,我需要一种方法来查看某个用户是否匹配可以包含通配符的主机掩码*。不使用正则表达式的最简单方法是什么?

一些例子:

    // Anything works
    *
            server.freenode.net ✔

    // Any nickname/user/host works
    *!*@*:
            any!thing@works ✔

    // Any nickname works (may have multiple nicknames on the same user)
    *!nebkat@unaffiliated/nebkat:
            nebkat!nebkat@unaffiliated/nebkat ✔
            123456!nebkat@unaffiliated/nebkat ✔
            nebkat!nebkat@unaffiliated/hacker ✘

    // Anything from an ip
    *!*@123.45.67.8:
            nebkat!nebkat@123.45.67.8 ✔
            123456!user2@123.45.67.8 ✔
            nebkat!nebkat@87.65.43.21 ✘

    // Anything where the username ends with nebkat
    *!*nebkat@*
            nebkat!nebkat@unaffiliated/nebkat ✔
            nebkat!prefix_nebkat@unaffiliated/nebkat ✔
            nebkat!nebkat_suffix@unaffiliated/nebkat ✘
4

2 回答 2

3

使用 org.apache.commons.io.FilenameUtils#wildcardMatch() 方法。更多细节在回答https://stackoverflow.com/a/43394347/466677

于 2017-04-13T14:04:01.083 回答
2

结束了这个:

public static boolean match(String host, String mask) {
    String[] sections = mask.split("\\*");
    String text = host;
    for (String section : sections) {
        int index = text.indexOf(section);
        if (index == -1) {
            return false;
        }
        text = text.substring(index + section.length());
    }
    return true;
}
于 2013-01-14T22:04:39.313 回答