gpt4 book ai didi

除 .hg_keep 之外的所有文件的正则表达式

转载 作者:行者123 更新时间:2023-12-01 13:03:45 25 4
gpt4 key购买 nike

我使用空的 .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

我只想保留 a/b/ 下的 .hg_keep 文件。

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

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

但 Mercurial 会忽略 b 子文件夹中的所有 .hg_keep 文件。

# 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)的解决方案?

最佳答案

正则表达式否定可能适用于此。如果您想忽略 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)$

将匹配其他一切。

关于除 .hg_keep 之外的所有文件的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4285200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com