是否有等效于java.util.regex的“ glob”类型模式?

是否存在用于在Java中进行“全局”类型匹配的标准(最好是Apache Commons或类似的非病毒)标准库?当我不得不在Perl中做类似的事情时,我只是将所有的“ .” 更改为“ \.”,将“ *”更改为“ .*”,将“ ?”更改为“ .”,但是我想知道是否有人做了为我工作。


天涯尽头无女友
浏览 478回答 3
3回答

犯罪嫌疑人X

这是一个处理*和?的简单Glob实现。在模式中public class GlobMatch {&nbsp; &nbsp; private String text;&nbsp; &nbsp; private String pattern;&nbsp; &nbsp; public boolean match(String text, String pattern) {&nbsp; &nbsp; &nbsp; &nbsp; this.text = text;&nbsp; &nbsp; &nbsp; &nbsp; this.pattern = pattern;&nbsp; &nbsp; &nbsp; &nbsp; return matchCharacter(0, 0);&nbsp; &nbsp; }&nbsp; &nbsp; private boolean matchCharacter(int patternIndex, int textIndex) {&nbsp; &nbsp; &nbsp; &nbsp; if (patternIndex >= pattern.length()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; switch(pattern.charAt(patternIndex)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case '?':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Match any character&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (textIndex >= text.length()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case '*':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // * at the end of the pattern will match anything&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (patternIndex + 1 >= pattern.length() || textIndex >= text.length()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Probe forward to see if we can get a match&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (textIndex < text.length()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (matchCharacter(patternIndex + 1, textIndex)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; textIndex++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (textIndex >= text.length()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String textChar = text.substring(textIndex, textIndex + 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String patternChar = pattern.substring(patternIndex, patternIndex + 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Note the match is case insensitive&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (textChar.compareToIgnoreCase(patternChar) != 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // End of pattern and text?&nbsp; &nbsp; &nbsp; &nbsp; if (patternIndex + 1 >= pattern.length() && textIndex + 1 >= text.length()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Go on to match the next character in the pattern&nbsp; &nbsp; &nbsp; &nbsp; return matchCharacter(patternIndex + 1, textIndex + 1);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java