RegexUtils { public static Pattern globToPattern(String glob) { ArgumentChecker.notNull(glob, "glob"); Map<Character, String> replacements = ImmutableMap.of('?', ".", '*', ".*?", '%', ".*?"); StringBuilder builder = new StringBuilder(); StringBuilder tokenBuilder = new StringBuilder(); for (int i = 0; i < glob.length(); i++) { char c = glob.charAt(i); if (!replacements.containsKey(c)) { tokenBuilder.append(c); } else { if (tokenBuilder.length() != 0) { String quotedToken = Pattern.quote(tokenBuilder.toString()); builder.append(quotedToken); tokenBuilder.setLength(0); } builder.append(replacements.get(c)); } } if (tokenBuilder.length() != 0) { builder.append(Pattern.quote(tokenBuilder.toString())); } return Pattern.compile(builder.toString()); } private  RegexUtils(); static Pattern wildcardsToPattern(final String text); static Pattern globToPattern(String glob); static boolean wildcardMatch(final String searchCriteriaWithWildcard, final String textToMatchAgainst); static boolean containsWildcard(final String searchCriteria); static String extract(String string, String regex); static String extract(String string, String regex, int group); static String extract(String string, Pattern pattern, int group); static boolean matches(String input, Pattern pattern);  }
@Test public void globToPattern() { assertEquals("\\Qfoo\\E.\\Qbar\\E", patternFor("foo?bar")); assertEquals(".\\Qfoobar\\E", patternFor("?foobar")); assertEquals("\\Qfoobar\\E.", patternFor("foobar?")); assertEquals("\\Qfoo\\E..\\Qbar\\E", patternFor("foo??bar")); assertEquals("..\\Qfoobar\\E", patternFor("??foobar")); assertEquals("\\Qfoobar\\E..", patternFor("foobar??")); assertEquals(".\\Qfoobar\\E.", patternFor("?foobar?")); assertEquals(".*?\\Qfoobar\\E.*?", patternFor("*foobar%")); assertEquals("\\Qfoo\\E.*?\\Qbar\\E", patternFor("foo*bar")); assertEquals("\\Qf\\E.\\Qoo\\E.*?\\Qbar\\E", patternFor("f?oo*bar")); assertEquals("\\Qf\\E.\\Qoo\\E.*?\\Qbar\\E", patternFor("f?oo%bar")); }