首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >文件路径通配符的BNF语法定义(glob)

文件路径通配符的BNF语法定义(glob)
EN

Stack Overflow用户
提问于 2014-09-07 14:51:15
回答 1查看 1.1K关注 0票数 7

我正在搜索一些用BFN规则描述的广泛扩展的方言(比如这个https://github.com/vmeurisse/wildmatch + globstar **)。

以任何形式或语言。OMeta或聚乙二醇会很好。

EN

回答 1

Stack Overflow用户

发布于 2014-09-19 20:25:02

我不确定是否理解您的问题,因为文件路径通配符的语法可以简化为一个简单的正则表达式。这个语法是由Unix定义的。

您可以在这里找到Bash的BNF:http://my.safaribooksonline.com/book/operating-systems-and-server-administration/unix/1565923472/syntax/lbs.appd.div.3

在programming语言中,glob.glob()函数的定义可以在文档中找到。该函数使用fnmatch.fnmatch()函数执行模式匹配。文档可以在这里获得:https://docs.python.org/2/library/fnmatch.html#fnmatch.fnmatch

fnmatch.fnmatch函数将文件路径通配符模式转换为典型的正则表达式,如下所示:

代码语言:javascript
运行
复制
def translate(pat):
    """Translate a shell PATTERN to a regular expression.

    There is no way to quote meta-characters.
    """

    i, n = 0, len(pat)
    res = ''
    while i < n:
        c = pat[i]
        i = i+1
        if c == '*':
            res = res + '.*'
        elif c == '?':
            res = res + '.'
        elif c == '[':
            j = i
            if j < n and pat[j] == '!':
                j = j+1
            if j < n and pat[j] == ']':
                j = j+1
            while j < n and pat[j] != ']':
                j = j+1
            if j >= n:
                res = res + '\\['
            else:
                stuff = pat[i:j].replace('\\','\\\\')
                i = j+1
                if stuff[0] == '!':
                    stuff = '^' + stuff[1:]
                elif stuff[0] == '^':
                    stuff = '\\' + stuff
                res = '%s[%s]' % (res, stuff)
        else:
            res = res + re.escape(c)
    return res + '\Z(?ms)'

能帮你写德BNF语法..。

编辑

这里有一个非常简单的语法:

代码语言:javascript
运行
复制
wildcard : expr
         | expr wildcard

expr : WORD
     | ASTERIX
     | QUESTION
     | neg_bracket_expr
     | pos_bracket_expr

pos_bracket_expr : LBRACKET WORD RBRACKET

neg_bracket_expr : LBRACKET EXCLAMATION WORD RBRACKET

由著名的ANTLR工具解析的流行语法列表可在这里获得:http://www.antlr3.org/grammar/list.html

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25711465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档