C# 有效文件名的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11794144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:17:45  来源:igfitidea点击:

Regular expression for valid filename

c#regex

提问by VJAI

I already gone through some question in StackOverflow regarding this but nothing helped much in my case.

我已经在 StackOverflow 中解决了一些关于此的问题,但在我的情况下没有任何帮助。

I want to restrict the user to provide a filename that should contain only alphanumeric characters, -, _, .and space.

我想限制用户提供只能包含字母数字字符-_.和空格的文件名。

I'm not good in regular expressions and so far I came up with this ^[a-zA-Z0-9.-_]$. Can somebody help me?

我不擅长正则表达式,到目前为止我想出了这个^[a-zA-Z0-9.-_]$。有人可以帮助我吗?

采纳答案by Engineer

This is the correct expression:

这是正确的表达:

string regex = @"^[\w\-. ]+$";

\wis equivalent of [0-9a-zA-Z_].

\w相当于[0-9a-zA-Z_]

回答by burning_LEGION

use this regular expression ^[a-zA-Z0-9._ -]+$

使用这个正则表达式 ^[a-zA-Z0-9._ -]+$

回答by Vinoth

To validate a file name i would suggest using the function provided by C# rather than regex

要验证文件名,我建议使用 C# 提供的函数而不是正则表达式

if (filename.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) != -1)
{
}

回答by Eric

This is a minor change to Engineers answer.

这是对工程师答案的一个小改动。

string regex = @"^[\w\- ]+[\w\-. ]*$"

This will block ".txt"which isn't valid.

这将阻止".txt"无效的。

Trouble is, it does block "..txt"which is valid

问题是,它确实阻止了"..txt"有效的

回答by Clem

Copied from @Engineer for future reference as the dot was not escaped (as it should) in the most voted answer.

从@Engineer 复制以备将来参考,因为点在投票最多的答案中没有被转义(应该是)。

This is the correct expression:

这是正确的表达:

string regex = @"^[\w\-\. ]+$";

回答by Johny Skovdal

While what the OP asks is close to what the currently accepted answer uses (^[\w\-. ]+$), there might be others seeing this question who has even more specific constraints.

虽然 OP 询问的内容与当前接受的答案使用的内容接近 ( ^[\w\-. ]+$),但可能会有其他人看到这个问题,他们有更具体的限制。

First off, running on a non-US/GB machine, \wwill allow a wide range of unwanted characters from foreign languages, according to the limitations of the OP.

首先\w,根据 OP 的限制,在非美国/GB 机器上运行将允许大量来自外语的不需要的字符。

Secondly, if the file extension is included in the name, this allows all sorts of weird looking, though valid, filenames like file .txtor file...txt.

其次,如果文件扩展名包含在名称中,则允许出现各种看起来很奇怪但有效的文件名,例如file .txtfile...txt

Thirdly, if you're simply uploading the files to your file system, you might want a blacklist of files and/or extensions like these:

第三,如果您只是将文件上传到您的文件系统,您可能需要如下文件和/或扩展名的黑名单:

web.config, hosts, .gitignore, httpd.conf, .htaccess

web.config、主机、.gitignore、httpd.conf、.htaccess

However, that is considerably out of scope for this question; it would require all sorts of info about the setup for good guidance on security issues. I thought I should raise the matter none the less.

然而,这远远超出了这个问题的范围。它需要有关设置的各种信息,以获得有关安全问题的良好指导。我想我还是应该提出这个问题。

So for a solution where the user can input the full file name, I would go with something like this:

因此,对于用户可以输入完整文件名的解决方案,我会采用以下方法:

^[a-zA-Z0-9](?:[a-zA-Z0-9 ._-]*[a-zA-Z0-9])?\.[a-zA-Z0-9_-]+$

It ensures that only the English alphabet is used, no beginning or trailing spaces, and ensures the use of a file extension with at least 1 in length and no whitespace.

它确保仅使用英文字母,没有开头或结尾空格,并确保使用长度至少为 1 且没有空格的文件扩展名。

I've tested this on Regex101, but for future reference, this was my "test-suite":

我已经在Regex101对此进行了测试,但为了将来参考,这是我的“测试套件”:

## THE BELOW SHOULD MATCH
web.config
httpd.conf
test.txt
1.1
my long file name.txt

## THE BELOW SHOULD NOT MATCH - THOUGH VALID
???.txt
hosts
.gitignore
.htaccess

回答by Krzysztof Karski

In case someone else needs to validate filenames (including Windows reserved words and such), here's a full expression: \A(?!(?:COM[0-9]|CON|LPT[0-9]|NUL|PRN|AUX|com[0-9]|con|lpt[0-9]|nul|prn|aux)|[\s\.])[^\\\/:*"?<>|]{1,254}\z

如果其他人需要验证文件名(包括 Windows 保留字等),这是一个完整的表达式: \A(?!(?:COM[0-9]|CON|LPT[0-9]|NUL|PRN|AUX|com[0-9]|con|lpt[0-9]|nul|prn|aux)|[\s\.])[^\\\/:*"?<>|]{1,254}\z

Extended expression (don't allow filenames starting with 2 dots, don't allow filenames ending in dots or whitespace):

扩展表达式(不允许文件名以 2 个点开头,不允许文件名以点或空格结尾):

\A(?!(?:COM[0-9]|CON|LPT[0-9]|NUL|PRN|AUX|com[0-9]|con|lpt[0-9]|nul|prn|aux)|\s|[\.]{2,})[^\\\/:*"?<>|]{1,254}(?<![\s\.])\z

\A(?!(?:COM[0-9]|CON|LPT[0-9]|NUL|PRN|AUX|com[0-9]|con|lpt[0-9]|nul|prn|aux)|\s|[\.]{2,})[^\\\/:*"?<>|]{1,254}(?<![\s\.])\z

Edit: For the interested, here's a link to Windows file naming conventions: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx

编辑:对于感兴趣的人,这里是 Windows 文件命名约定的链接:https: //msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx

回答by luky

I've just created this. It prevents two dots and dot at end and beginning. It doesn't allow any two dots though.

我刚刚创建了这个。它可以防止在结尾和开头出现两个点和点。但它不允许任何两个点。

^([a-zA-Z0-9_]+)\.(?!\.)([a-zA-Z0-9]{1,5})(?<!\.)$

回答by mike rodent

I may be saying something stupid here, but it seems to me that these answers aren't correct. Firstly, are we talking Linux or Windows here (or another OS)?

我可能在这里说一些愚蠢的话,但在我看来,这些答案是不正确的。首先,我们在这里谈论的是 Linux 还是 Windows(或其他操作系统)?

Secondly, in Windows it is (I believe) perfectly legitimate to include a "$" in a filename, not to mention Unicode in general. It certainly seems possible.

其次,在 Windows 中(我相信)在文件名中包含“$”是完全合法的,更不用说一般的 Unicode。这似乎是可能的。

I tried to get a definitive source on this... and ending up at the Wikip Filename page: in particular the section "Reserved characters and words" seems relevant: and these are, clearly, a list of things which you are NOT allowed to put in.

我试图获得一个明确的来源......并在Wikip 文件名页面结束:特别是“保留字符和单词”部分似乎相关:显然,这些是您不允许的事情列表投放。

I'm in the Java world. And I naturally assumed that Apache Commons would have something like validateFilename, maybe in FilenameUtils... but it appears not (if it had done, this would still be potentially useful to C# programmers, as the code is usually pretty easy to understand, and could therefore be translated). I did do an experiment, though, using the method normalize: to my disappointment it allowed perfectly invalid characters (?, etc.) to "pass".

我在 Java 世界。而且我很自然地假设 Apache Commons 会有类似的东西validateFilename,也许在FilenameUtils……但它似乎没有(如果它已经完成,这对 C# 程序员仍然可能有用,因为代码通常很容易理解,因此可以翻译)。不过,我确实使用该方法做了一个实验normalize:令我失望的是,它允许完全无效的字符(?等)“通过”。

The part of the Wikip Filename page referenced above shows that this question depends on the OS you're using... but it should be possible to concoct some simple regex for Linux and Windows at least.

上面引用的 Wikip 文件名页面的部分表明这个问题取决于您使用的操作系统......但至少应该可以为 Linux 和 Windows 编造一些简单的正则表达式。

Then I found a Java way (at least):

然后我找到了一种Java方式(至少):

Path path = java.nio.file.FileSystems.getDefault().getPath( 'bobb??::mouse.blip' );

output:

输出:

java.nio.file.InvalidPathException: Illegal char at index 4: bobb??::mouse.blip

java.nio.file.InvalidPathException:索引 4 处的非法字符:bobb??::mouse.blip

... presumably different FileSystemobjects will have different validation rules

...大概不同的FileSystem对象会有不同的验证规则

回答by robs

For full character set (Unicode) use ^[\p{L}0-9_\-.~]+$

对于完整字符集 (Unicode) 使用 ^[\p{L}0-9_\-.~]+$

or perhaps ^[\p{L}\p{N}_\-.~]+$would be more accurate if we are talking about Unicode.

或者^[\p{L}\p{N}_\-.~]+$如果我们谈论 Unicode可能 会更准确。

I added a '~' simply because I have some files using that character.

我添加了一个 '~' 只是因为我有一些使用该字符的文件。