正则表达式像gitignore的外壳glob模式

编程入门 行业动态 更新时间:2024-10-27 06:20:26
本文介绍了正则表达式像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模式

本文发布于:2023-11-29 17:43:57,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:外壳   模式   正则表达式   gitignore   glob

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!