3

我使用空.hg_keep文件在 Mercurial 中保留一些(否则为空的)文件夹。

问题是我找不到一个工作正则表达式,它排除了除.hg_keep文件之外的所有内容。

假设我们有这个文件结构:

a/b/c2/.hg_keep
a/b/c/d/.hg_keep
a/b/c/d/file1
a/b/c/d2/.hg_keep
a/b/.hg_keep
a/b/file2
a/b/file1
a/.hg_keep
a/file2
a/file1

我只想.hg_keep保留a/b/.

在http://gskinner.com/RegExr/的帮助下,我创建了以下内容.hgignore

syntax: regexp
.*b.*/(?!.*\.hg_keep)

但 Mercurial.hg_keep会忽略b.

# hg status
? .hgignore
? a/.hg_keep
? a/b/.hg_keep
? a/file1
? a/file

# hg status -i
I a/b/c/d/.hg_keep
I a/b/c/d/file1
I a/b/c/d2/.hg_keep
I a/b/c2/.hg_keep
I a/b/file1
I a/b/file2

我知道我可以hd add保存所有.hg_keep文件,但是有正则表达式(或 glob)的解决方案吗?

4

4 回答 4

3

正则表达式否定可能适用于此。如果您想忽略文件之外的所有内容a/b/.hg_keep,您可以使用:

^(?!a/b/\.hg_keep)$

这个正则表达式的重要部分是:

^                   anchor the match to the beginning of the file path
(?!  ... )          negation of the expression between '!' and ')'
a/b/\.hg_keep       the full path of the file you want to match
$                   anchor the match to the end of the file path

正则表达式

^a/b/\.hg_keep$

匹配名为a/b/.hg_keep.

它的否定

^(?!a/b/\.hg_keep)$

将匹配其他所有内容。

于 2011-01-31T04:26:04.993 回答
1

不太确定您在什么上下文中使用正则表达式,但这应该是它,这匹配所有以 结尾的行.hg_keep

^.*\.hg_keep$

编辑:这是一个正则表达式来匹配与上述表达式不匹配的项目:

^(?:(?!.*\.hg_keep).)*$
于 2010-11-26T12:17:29.093 回答
0

试试(?!.*/\.hg_keep$)

于 2010-11-26T12:19:03.593 回答
0

寻找与此类似的东西。找到了答案,但这不是我们想听到的。

  1. 限制

除了一组文件之外,没有直接的方法可以忽略所有文件。与其他模式结合使用时,尝试使用反向正则表达式匹配将失败。这是一个有意的限制,因为所有替代格式都被认为太可能使用户感到困惑,因此值得额外的灵活性。

参考:https ://www.mercurial-scm.org/wiki/.hgignore

于 2015-11-10T04:04:57.197 回答