54

我已经在 StackOverflow 中解决了一些关于此问题的问题,但对我来说没有什么帮助。

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

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

4

10 回答 10

76

这是正确的表达方式:

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

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

于 2012-08-03T11:06:25.287 回答
33

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

if (filename.IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) != -1)
{
}
于 2012-08-03T10:52:46.123 回答
14

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.

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

Secondly, if the file extension is included in the name, this allows all sorts of weird looking, though valid, filenames like file .txt or file...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

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.

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

## 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
于 2017-01-03T14:55:02.083 回答
11

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

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

\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

于 2017-08-10T13:55:38.260 回答
2

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

于 2012-08-03T10:46:03.907 回答
2

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

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

这将阻止".txt"无效。

麻烦的是,它确实阻止"..txt"了有效的

于 2012-11-08T15:36:35.793 回答
2

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

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

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

于 2019-10-18T07:04:22.223 回答
1

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})(?<!\.)$
于 2017-09-01T10:19:46.240 回答
0

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)?

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.

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.

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".

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.

Then I found a Java way (at least):

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

output:

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

... presumably different FileSystem objects will have different validation rules

于 2018-02-15T18:44:05.803 回答
-3

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

This is the correct expression:

string regex = @"^[\w\-\. ]+$";
于 2016-02-03T15:22:32.497 回答