static String toRegularExpression(String glob) {
        StringBuilder output = new StringBuilder("^");
        boolean literal = true;
        boolean processingGroup = false;

        for (int i = 0; i < glob.length(); ) {
            char c = glob.charAt(i++);
            switch (c) {
                case '?':
                    literal = false;
                    output.append(".");
                    break;
                case '*':
                    literal = false;
                    output.append(".*");
                    break;
                case '\\':
                    if (i == glob.length()) {
                        output.append(c);
                    } else {
                        char next = glob.charAt(i);
                        i++;
                        if (isGlobSpecialCharacter(next) ||
                                isRegularExpressionSpecialCharacter(next)) {
                            output.append('\\');
                        }
                        output.append(next);
                    }
                    break;
                case '{':
                    if (processingGroup) {
                        throw new RuntimeException("Can't nest glob groups.");
                    }
                    literal = false;
                    output.append("(?:(?:");
                    processingGroup = true;
                    break;
                case ',':
                    if (processingGroup) {
                        literal = false;
                        output.append(")|(?:");
                    } else {
                        output.append(c);
                    }
                    break;
                case '}':
                    if (processingGroup) {
                        literal = false;
                        output.append("))");
                        processingGroup = false;
                    } else {
                        output.append(c);
                    }
                    break;
                // TODO: handle character ranges
                default:
                    if (isRegularExpressionSpecialCharacter(c)) {
                        output.append('\\');
                    }
                    output.append(c);
            }
        }
        if (processingGroup) {
            throw new RuntimeException("Unterminated glob group.");
        }
        if (literal) {
            return null;
        }
        output.append('$');
        return output.toString();
    }
@Test
    public void testToRegularExpression() {
        assertNull(GlobComponent.toRegularExpression("blah"));
        assertNull(GlobComponent.toRegularExpression(""));
        assertNull(GlobComponent.toRegularExpression("does not need a regex, actually"));
        assertEquals("^\\$blah.*$", GlobComponent.toRegularExpression("$blah*"));
        assertEquals("^.*$", GlobComponent.toRegularExpression("*"));
        assertEquals("^foo(?:(?:bar)|(?:baz))$", GlobComponent.toRegularExpression("foo{bar,baz}"));
    }