正则表达式匹配两个子目录中的任何一个目录深吗?

时间:2015-10-30 03:03:14

标签: python regex

我想忽略所有.git子目录和test子目录。

更大的问题是我是否可以用更可读的方式来写这个,而不是正则表达式。

我希望指定compileall

rx参数

2 个答案:

答案 0 :(得分:1)

如果rx参数不需要正则表达式,则可以使用满足所需跳过条件的名为search的类方法构造一个类:

class MyRx(object):
    @classmethod
    def search(cls, p):
        return 'test' in p or '.git' in p

用法

compileall.compile_dir('.', rx=MyRx, ...)

答案 1 :(得分:0)

这并没有解决是否可以向rx提供回调方法的更大问题,但这似乎对我有用:

compileall.compile_dir('.', maxlevels=20, force=True, \
                       rx=re.compile(r'([/\\][.]git|[/\\]test)'), quiet=False, \
                       legacy=False, optimize=0)

有关正则表达式运算符的一些文档:

http://metahtml.sourceforge.net/documentation/regex/regex_3.mhtml