Using regex: Is there a function to convert an Ant-style glob pattern into a regex? on newest questions tagged regex – Stack Overflow

I know I can write one myself, but I was hoping I can just reuse an existing one.

Ant-style globs are pretty much the same as normal file globs with the addition that ‘**’ matches subdirectories.

FWIW, my implementation is:

public class AntGlobConverter {
    public static String convert(String globExpression) {
        StringBuilder result = new StringBuilder();

        for (int i = 0; i != globExpression.length(); ++i) {
            final char c = globExpression.charAt(i);

            if (c == '?') {
                result.append('.');
            } else if (c == '*') {
                if (i + 1 != globExpression.length() && globExpression.charAt(i + 1) == '*') {
                    result.append(".*");
                    ++i;
                } else {
                    result.append("[^/]*");
                }
            } else {
                result.append(c);
            }
        }

        return result.toString();
    }
}

See Answers


source: http://stackoverflow.com/questions/8582665/is-there-a-function-to-convert-an-ant-style-glob-pattern-into-a-regex
Using regex: using-regex



online applications demo