本文介绍了是否有将Ant样式的glob模式转换为正则表达式的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以自己写一个,但是我希望可以重用一个已有的.

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

Ant样式的glob与普通文件的glob几乎相同,不同之处在于'**'匹配子目录.

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

FWIW,我的实现是:

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();
    }
}

推荐答案

我最近亲自遇到了这个问题,并编写了一种进行转换的方法.正确执行此操作比上面的问题中的代码复杂.

I recent came across this problem myself and wrote a method to do the conversion. To do it properly was more complicated than the code above in the question.

请参见convertAntGlobToRegEx方法" rel ="nofollow noreferrer> https://github.com/bndtools/bnd/blob/master/aQute.libg/src/aQute/libg/glob/AntGlob.java .

See the convertAntGlobToRegEx method in https://github.com/bndtools/bnd/blob/master/aQute.libg/src/aQute/libg/glob/AntGlob.java.

请参见 https://github.com/bndtools/bnd/blob/master/aQute.libg/test/aQute/libg/glob/AntGlobTest.java 用于测试用例.

这篇关于是否有将Ant样式的glob模式转换为正则表达式的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 04:52