hgignore:帮助忽略所有文件,但某些文件除外 [英] hgignore: help ignoring all files but certain ones
问题描述
我需要一个.hgdontignore文件:-)来包括某些文件,并排除目录中的所有其他文件.基本上,我只希望在特定目录中仅包含.jar文件,而不要包含其他文件.我怎样才能做到这一点?我对正则表达式语法不太熟练.还是可以使用glob语法来做到这一点? (我更喜欢这样做是为了提高可读性)
I need an .hgdontignore file :-) to include certain files and exclude everything else in a directory. Basically I want to include only the .jar files in a particular directory and nothing else. How can I do this? I'm not that skilled in regular expression syntax. Or can I do it with glob syntax? (I prefer that for readability)
仅作为示例位置,假设我要排除foo/bar/
下的所有文件,除了foo/bar/*.jar
.
Just as an example location, let's say I want to exclude all files under foo/bar/
except for foo/bar/*.jar
.
推荐答案
为此,您需要使用以下正则表达式:
To do this, you'll need to use this regular expression:
foo/bar/.+?\.(?!jar).+
说明
您正在告诉它要忽略的内容,因此该表达式正在搜索您不需要想要的东西.
You are telling it what to ignore, so this expression is searching for things you don't want.
- 您要查找名称(包括相对目录)包括(foo/bar/)的任何文件
- 然后,您要查找句点之前的所有字符(.+?\.==可以在任何时候匹配一个或多个字符,直到达到句点字符为止)
- 然后确保没有"jar"结尾(?!jar)(这被称为否定前瞻
- 最后,您抓住它确实具有(.+)的结尾
正则表达式很容易弄乱,因此我强烈建议您使用 Regex Buddy 之类的工具.帮助您建立它们.它将把正则表达式分解为简单的英语,这确实有帮助.
Regular expressions are easy to mess up, so I strongly suggest that you get a tool like Regex Buddy to help you build them. It will break down a regex into plain English which really helps.
编辑
嘿 Jason S ,您抓到我了,它确实错过了那些文件.
Hey Jason S, you caught me, it does miss those files.
此更正后的正则表达式将适用于您列出的每个示例:
This corrected regex will work for every example you listed:
foo/bar/(?!.*\.jar$).+
它发现:
- foo/bar/baz.txt
- foo/bar/baz
- foo/bar/jar
- foo/bar/baz.jar.txt
- foo/bar/baz.jar.
- foo/bar/baz.
- foo/bar/baz.txt.
但找不到
- foo/bar/baz.jar
新说明
这表示在"foo/bar/"中查找文件,然后如果有零个或多个字符后跟".jar",然后没有其他字符($表示该行的末尾),则不匹配,然后,如果并非如此,请匹配以下任何字符.
This says look for files in "foo/bar/" , then do not match if there are zero or more characters followed by ".jar" and then no more characters ($ means end of the line), then, if that isn't the case, match any following characters.
这篇关于hgignore:帮助忽略所有文件,但某些文件除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!