正则表达式捕获除以“.”开头的所有文件

发布于 2024-09-03 03:22:48 字数 348 浏览 2 评论 0原文

在包含混合内容的目录中,例如:

.afile
.anotherfile
bfile.file
bnotherfile.file
.afolder/
.anotherfolder/
bfolder/
bnotherfolder/

如何捕获除以 . 开头的文件(而不是目录)之外的所有内容?
我尝试过使用负前瞻 ^(?!\.).+? 但它似乎无法正常工作。
请注意,我想通过使用 [a-zA-Z< 排除 . 来避免这样做。加上所有其他可能的字符减去点>]

有什么建议吗?

In a directory with mixed content such as:

.afile
.anotherfile
bfile.file
bnotherfile.file
.afolder/
.anotherfolder/
bfolder/
bnotherfolder/

How would you catch everything but the files (not dirs) starting with .?
I have tried with a negative lookahead ^(?!\.).+? but it doesn't seem to work right.
Please note that I would like to avoid doing it by excluding the . by using [a-zA-Z< plus all other possible chars minus the dot >]

Any suggestions?

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

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

发布评论

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

评论(4

淤浪 2024-09-10 03:22:48

这应该可以做到:

^[^.].*$

[^abc] 将匹配任何不是 a、b 或 c

This should do it:

^[^.].*$

[^abc] will match anything that is not a, b or c

開玄 2024-09-10 03:22:48

转义 . 并否定可以开始您所拥有的名称的字符:

^[^\.].*$

使用您的测试用例成功测试

Escaping .and negating the characters that can start the name you have:

^[^\.].*$

Tested successfully with your test cases here.

初与友歌 2024-09-10 03:22:48

负向先行 ^(?!\.).+$ 确实有效。这是 Java 版本:

    String[] files = {
        ".afile",
        ".anotherfile",
        "bfile.file",
        "bnotherfile.file",
        ".afolder/",
        ".anotherfolder/",
        "bfolder/",
        "bnotherfolder/",
        "", 
    };
    for (String file : files) {
        System.out.printf("%-18s %6b%6b%n", file,
            file.matches("^(?!\\.).+$"),
            !file.startsWith(".")
        );
    }

输出为(如 ideone.com 上所示):

.afile              false false
.anotherfile        false false
bfile.file           true  true
bnotherfile.file     true  true
.afolder/           false false
.anotherfolder/     false false
bfolder/             true  true
bnotherfolder/       true  true
                    false  true

另请注意非正则表达式 String.startsWith。可以说这是最好、最易读的解决方案,因为无论如何都不需要正则表达式,并且 startsWithO(1) 其中正则表达式(至少在 Java 中)是 <代码>O(N)。

请注意空白字符串上的分歧。如果这是一个可能的输入,并且您希望它返回 false,您可以编写如下内容:

!file.isEmpty() && !file.startsWith(".")

另请参阅

The negative lookahead ^(?!\.).+$ does work. Here it is in Java:

    String[] files = {
        ".afile",
        ".anotherfile",
        "bfile.file",
        "bnotherfile.file",
        ".afolder/",
        ".anotherfolder/",
        "bfolder/",
        "bnotherfolder/",
        "", 
    };
    for (String file : files) {
        System.out.printf("%-18s %6b%6b%n", file,
            file.matches("^(?!\\.).+$"),
            !file.startsWith(".")
        );
    }

The output is (as seen on ideone.com):

.afile              false false
.anotherfile        false false
bfile.file           true  true
bnotherfile.file     true  true
.afolder/           false false
.anotherfolder/     false false
bfolder/             true  true
bnotherfolder/       true  true
                    false  true

Note also the use of the non-regex String.startsWith. Arguably this is the best, most readable solution, because regex is not needed anyway, and startsWith is O(1) where as the regex (at least in Java) is O(N).

Note the disagreement on the blank string. If this is a possible input, and you want this to return false, you can write something like this:

!file.isEmpty() && !file.startsWith(".")

See also

枯叶蝶 2024-09-10 03:22:48

嗯...负面角色类别怎么样?

[^.]

排除点?

Uhm... how about a negative character class?

[^.]

to exclude the dot?

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