正则表达式像gitignore的外壳glob模式 [英] Regex-like shell glob patterns for gitignore

查看:111
本文介绍了正则表达式像gitignore的外壳glob模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编译我的C ++项目时,许多共享目标文件是用扩展名创建的,例如

  .so 
.so.0
.so.7
.so.0.7

我需要将所有这些添加到我的 .gitignore 文件中。如果这是一个正则表达式,我可以使用

$ $ p $ $ $ $ $ $ $
$ / code>

然而,文档表示 .gitignore


将模式视为code> fnmatch(3)与 FNM_PATHNAME 标志


我发现没有办法按照我找到的 fnmatch 文档进行操作。是否真的没有办法做到这一点?

解决方案

虽然@SpeakEasy的回答可以忽略 .so 文件在使用 *。so * 的单个步骤中,对于忽略指定格式的文件的用例,可以在 .gitignore 更具体的忽略规则

  *。so 
* .so。[0-9] *






gitignore手册页


gitignore文件中的每行指定一个模式。



Git将模式视为适合于通过 fnmatch

重要的是要注意该模式与正则表达式不同。



Python有一个模块n amed fnmatch ,您可以使用它来验证是否

示例示例:

  import fnmatch 
pattern =* .so。[0-9] *
filenames = [a.so,b.so.0,b.so.11, b.so.1.0,b.so.1.0.12]

用于文件名中的文件名:
打印文件名,fnmatch.fnmatch(文件名,模式)

>>> a.so False
b.so.0 True
b.so.11 True
b.so.1.0 True
b.so.1.0.12 True


When I compile my C++ project, many shared object files are created with extensions such as

.so
.so.0
.so.7
.so.0.7

I need to add all those to my .gitignore file. Were this a regex, I could use

\.so[\.0-9]*

However, the documentation says that .gitignore

treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag

I found no way to do what I want with the fnmatch documentations I found. Is there really no way to do this?

解决方案

While the answer by @SpeakEasy can ignore .so files in a single step using *.so*, for your use case of ignoring files in formats specified, you can use two entries in your .gitignore for more specific ignore rule

*.so
*.so.[0-9]*


From gitignore man page

Each line in a gitignore file specifies a pattern.

Git treats the pattern as a shell glob suitable for consumption by fnmatch

The important thing to note is that the pattern is not the same as regular expressions.

Python has a module named fnmatch, you can use that to verify whether a particular filename matches the pattern or not.

Sample example:

import fnmatch
pattern = "*.so.[0-9]*"
filenames = ["a.so", "b.so.0", "b.so.11", "b.so.1.0", "b.so.1.0.12"]

for filename in filenames:
    print filename, fnmatch.fnmatch(filename, pattern)

>>> a.so False
    b.so.0 True
    b.so.11 True
    b.so.1.0 True
    b.so.1.0.12 True

这篇关于正则表达式像gitignore的外壳glob模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆