@Test public void verify_match() { Pattern pattern = GlobHelper.translate(glob, "\\/"); boolean matches = pattern.matcher(line).matches(); Assert.assertTrue(glob + " match " + line + " -> " + match, matches == match); }
public static Pattern translate(String pattern, String separator) { StringBuffer sb = new StringBuffer(); String es = escape(separator); Matcher ss = Pattern.compile("^([*][*][" + es + "]).*").matcher(pattern); if (ss.matches()) { pattern = pattern.substring(ss.group(1).length()); sb.append("(.*[" + es + "])?"); } Matcher st = Pattern.compile(".*([" + es + "][*][*])$").matcher(pattern); boolean useSt = false; if (st.matches()) { pattern = pattern.substring(0, st.start(1)); useSt = true; } for(int i = 0; i != pattern.length(); ++i) { char c = pattern.charAt(i); if (c == '?') { sb.append("[^" + es + "]"); } else if (c == '*') { if (i + 1 < pattern.length() && pattern.charAt(i+1) == '*') { i++; sb.append(".*"); } else { sb.append("[^" + es + "]*"); } } else { if (c == '$') { sb.append("\\$"); } else if (Character.isJavaIdentifierPart(c) || Character.isWhitespace(c)) { sb.append(c); } else { sb.append('\\').append(c); } } } if (useSt) { sb.append("([" + es + "].*)?"); } return Pattern.compile(sb.toString()); }
GlobHelper { public static Pattern translate(String pattern, String separator) { StringBuffer sb = new StringBuffer(); String es = escape(separator); Matcher ss = Pattern.compile("^([*][*][" + es + "]).*").matcher(pattern); if (ss.matches()) { pattern = pattern.substring(ss.group(1).length()); sb.append("(.*[" + es + "])?"); } Matcher st = Pattern.compile(".*([" + es + "][*][*])$").matcher(pattern); boolean useSt = false; if (st.matches()) { pattern = pattern.substring(0, st.start(1)); useSt = true; } for(int i = 0; i != pattern.length(); ++i) { char c = pattern.charAt(i); if (c == '?') { sb.append("[^" + es + "]"); } else if (c == '*') { if (i + 1 < pattern.length() && pattern.charAt(i+1) == '*') { i++; sb.append(".*"); } else { sb.append("[^" + es + "]*"); } } else { if (c == '$') { sb.append("\\$"); } else if (Character.isJavaIdentifierPart(c) || Character.isWhitespace(c)) { sb.append(c); } else { sb.append('\\').append(c); } } } if (useSt) { sb.append("([" + es + "].*)?"); } return Pattern.compile(sb.toString()); } }
GlobHelper { public static Pattern translate(String pattern, String separator) { StringBuffer sb = new StringBuffer(); String es = escape(separator); Matcher ss = Pattern.compile("^([*][*][" + es + "]).*").matcher(pattern); if (ss.matches()) { pattern = pattern.substring(ss.group(1).length()); sb.append("(.*[" + es + "])?"); } Matcher st = Pattern.compile(".*([" + es + "][*][*])$").matcher(pattern); boolean useSt = false; if (st.matches()) { pattern = pattern.substring(0, st.start(1)); useSt = true; } for(int i = 0; i != pattern.length(); ++i) { char c = pattern.charAt(i); if (c == '?') { sb.append("[^" + es + "]"); } else if (c == '*') { if (i + 1 < pattern.length() && pattern.charAt(i+1) == '*') { i++; sb.append(".*"); } else { sb.append("[^" + es + "]*"); } } else { if (c == '$') { sb.append("\\$"); } else if (Character.isJavaIdentifierPart(c) || Character.isWhitespace(c)) { sb.append(c); } else { sb.append('\\').append(c); } } } if (useSt) { sb.append("([" + es + "].*)?"); } return Pattern.compile(sb.toString()); }  }
GlobHelper { public static Pattern translate(String pattern, String separator) { StringBuffer sb = new StringBuffer(); String es = escape(separator); Matcher ss = Pattern.compile("^([*][*][" + es + "]).*").matcher(pattern); if (ss.matches()) { pattern = pattern.substring(ss.group(1).length()); sb.append("(.*[" + es + "])?"); } Matcher st = Pattern.compile(".*([" + es + "][*][*])$").matcher(pattern); boolean useSt = false; if (st.matches()) { pattern = pattern.substring(0, st.start(1)); useSt = true; } for(int i = 0; i != pattern.length(); ++i) { char c = pattern.charAt(i); if (c == '?') { sb.append("[^" + es + "]"); } else if (c == '*') { if (i + 1 < pattern.length() && pattern.charAt(i+1) == '*') { i++; sb.append(".*"); } else { sb.append("[^" + es + "]*"); } } else { if (c == '$') { sb.append("\\$"); } else if (Character.isJavaIdentifierPart(c) || Character.isWhitespace(c)) { sb.append(c); } else { sb.append('\\').append(c); } } } if (useSt) { sb.append("([" + es + "].*)?"); } return Pattern.compile(sb.toString()); }  static Pattern translate(String pattern, String separator); }
GlobHelper { public static Pattern translate(String pattern, String separator) { StringBuffer sb = new StringBuffer(); String es = escape(separator); Matcher ss = Pattern.compile("^([*][*][" + es + "]).*").matcher(pattern); if (ss.matches()) { pattern = pattern.substring(ss.group(1).length()); sb.append("(.*[" + es + "])?"); } Matcher st = Pattern.compile(".*([" + es + "][*][*])$").matcher(pattern); boolean useSt = false; if (st.matches()) { pattern = pattern.substring(0, st.start(1)); useSt = true; } for(int i = 0; i != pattern.length(); ++i) { char c = pattern.charAt(i); if (c == '?') { sb.append("[^" + es + "]"); } else if (c == '*') { if (i + 1 < pattern.length() && pattern.charAt(i+1) == '*') { i++; sb.append(".*"); } else { sb.append("[^" + es + "]*"); } } else { if (c == '$') { sb.append("\\$"); } else if (Character.isJavaIdentifierPart(c) || Character.isWhitespace(c)) { sb.append(c); } else { sb.append('\\').append(c); } } } if (useSt) { sb.append("([" + es + "].*)?"); } return Pattern.compile(sb.toString()); }  static Pattern translate(String pattern, String separator);  }