/** returns true iff the target matches the given pattern, * under simplified bash rules -- viz permitting * and ? and comma delimited patterns inside curly braces * @throws InvalidPatternException */ public static boolean isGlobMatched(String globPattern, String targetText) throws InvalidPatternException { List<String> patterns = getGlobsAfterBraceExpansion(globPattern); for (String p : patterns) { if (isNoBraceGlobMatched(p, targetText)) return true; } return false; }
/** returns true iff the target matches the given pattern, * under simplified bash rules -- viz permitting * and ? and comma delimited patterns inside curly braces, * as well as things like {1,2,5-10} (and also {01,02,05-10} to keep leading 0) * @throws InvalidPatternException */ public boolean isGlobMatchedNumeric(String globPattern, String targetText) throws InvalidPatternException { List<String> patterns = expand(globPattern); for (String p : patterns) { if (isNoBraceGlobMatched(p, targetText)) return true; } return false; }
/** whether a glob-ish string without braces (e.g. containing just ? and * chars) matches; * can be used directly, also used implicitly by isGlobMatched after glob expansion */ public static boolean isNoBraceGlobMatched(String globPattern, String target) { int pi=0, ti=0; while (pi<globPattern.length() && ti<target.length()) { char pc = globPattern.charAt(pi); char tc = target.charAt(pi); if (pc=='?') { pi++; ti++; continue; } if (pc!='*') { if (pc!=tc) return false; pi++; ti++; continue; } //match 0 or more chars String prest = globPattern.substring(pi+1); while (ti<=target.length()) { if (isNoBraceGlobMatched(prest, target.substring(ti))) return true; ti++; } return false; } while (pi<globPattern.length() && globPattern.charAt(pi)=='*') pi++; return (pi==globPattern.length() && ti==target.length()); }