String (@NotNull String antPattern, boolean ignoreStartingSlash) { StringBuilder builder = new StringBuilder(); int asteriskCount = 0; boolean recursive = true; int start = ignoreStartingSlash && (StringUtil.startsWithChar(antPattern, '/') || StringUtil.startsWithChar(antPattern, '\\')) ? 1 : 0; for (int idx = start; idx < antPattern.length(); idx++) { char ch = antPattern.charAt(idx); if (ch == '*') { asteriskCount++; continue; } boolean foundRecursivePattern = recursive && asteriskCount == 2 && (ch == '/' || ch == '\\'); boolean asterisksFound = asteriskCount > 0; asteriskCount = 0; recursive = ch == '/' || ch == '\\'; if (foundRecursivePattern) { builder.append("(?:[^/]+/)*?"); continue; } if (asterisksFound) { builder.append("[^/]*?"); } if (ch == '(' || ch == ')' || ch == '[' || ch == ']' || ch == '^' || ch == '$' || ch == '.' || ch == '{' || ch == '}' || ch == '+' || ch == '|') { // quote regexp-specific symbols builder.append('\\').append(ch); continue; } if (ch == '?') { builder.append("[^/]{1}"); continue; } if (ch == '\\') { builder.append('/'); continue; } builder.append(ch); } // handle ant shorthand: my_package/test/ is interpreted as if it were my_package/test/** boolean isTrailingSlash = builder.length() > 0 && builder.charAt(builder.length() - 1) == '/'; if (asteriskCount == 0 && isTrailingSlash || recursive && asteriskCount == 2) { if (isTrailingSlash) { builder.setLength(builder.length() - 1); } if (builder.length() == 0) { builder.append(".*"); } else { builder.append("(?:$|/.+)"); } } else if (asteriskCount > 0) { builder.append("[^/]*?"); } return builder.toString(); }