我正在创建一个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.4.567.89:
nebkat!nebkat@123.4.567.89 ✔
123456!user2@123.4.567.89 ✔
nebkat!nebkat@98.765.4.321 ✘
// Anything where the username ends with nebkat
*!*nebkat@*
nebkat!nebkat@unaffiliated/nebkat ✔
nebkat!prefix_nebkat@unaffiliated/nebkat ✔
nebkat!nebkat_suffix@unaffiliated/nebkat ✘
答案 0 :(得分: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;
}
答案 1 :(得分:0)
使用org.apache.commons.io.FilenameUtils #worldcardMatch()方法。更多细节在回答https://stackoverflow.com/a/43394347/466677。