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

发布于 2024-10-05 03:19:39 字数 940 浏览 0 评论 0 原文

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

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

I use empty .hg_keep files to keep some (otherwise empty) folders in Mercurial.

The problem is that I can't find a working regex which excludes everything but the .hg_keep files.

lets say we have this filestructure:

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

and I want to keep only the .hg_keep files under a/b/.

with the help of http://gskinner.com/RegExr/ I created the following .hgignore:

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

but Mercurial ignores all .hg_keep files in subfolders of 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

I know that I a can hd add all the .hg_keep files, but is there a solution with a regular expression (or glob)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

∞觅青森が 2024-10-12 03:19:39

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

将匹配 < em>仅名为a/b/.hg_keep的文件。

它的否定

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

将匹配其他一切。

Regexp negation might work for this. If you want to ignore everything except the a/b/.hg_keep file, you can probably use:

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

The parts of this regexp that matter are:

^                   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

The regular expression

^a/b/\.hg_keep$

would match only the file called a/b/.hg_keep.

Its negation

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

will match everything else.

睫毛上残留的泪 2024-10-12 03:19:39

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

^.*\.hg_keep$

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

^(?:(?!.*\.hg_keep).)*$

Not quite sure in what context you are using the Regex but this should be it, this matches all lines ending in .hg_keep:

^.*\.hg_keep$

EDIT: And here is a Regex to match items not matching the above expression:

^(?:(?!.*\.hg_keep).)*$
2024-10-12 03:19:39

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

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

似狗非友 2024-10-12 03:19:39

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

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

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


    Looking for something similiar to this.
    Found an answer, but it's not what we want to hear.

    1. Limitations

    There is no straightforward way to ignore all but a set of files. Attempting to use an inverted regex match will fail when combined with other patterns. This is an intentional limitation, as alternate formats were all considered far too likely to confuse users to be worth the additional flexibility.

    Ref: https://www.mercurial-scm.org/wiki/.hgignore

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文