C# 通过正则表达式验证文件类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/374930/
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
Validating file types by regular expression
提问by mmattax
I have a .NET webform that has a file upload control that is tied to a regular expression validator. This validator needs to validate that only certain filetypes should be allowed for upload (jpg,gif,doc,pdf)
我有一个 .NET webform,它有一个与正则表达式验证器相关联的文件上传控件。此验证器需要验证只允许上传某些文件类型(jpg、gif、doc、pdf)
The current regular expression that does this is:
当前执行此操作的正则表达式是:
^(([a-zA-Z]:)|(\{2}\w+)$?)(\(\w[\w].*))(.jpg|.JPG|.gif|.GIF|.doc|.DOC|.pdf|.PDF)$
However this does not seem to be working... can anyone give me a little reg ex help?
然而,这似乎不起作用......谁能给我一点reg ex帮助?
采纳答案by Dario Solera
Your regex seems a bit too complex in my opinion. Also, remember that the dot is a special character meaning "any character". The following regex should work (note the escaped dots):
在我看来,您的正则表达式似乎有点过于复杂。另外,请记住点是一个特殊字符,意思是“任何字符”。以下正则表达式应该可以工作(注意转义点):
^.*\.(jpg|JPG|gif|GIF|doc|DOC|pdf|PDF)$
You can use a tool like Expressoto test your regular expressions.
您可以使用Expresso 之类的工具来测试您的正则表达式。
回答by Joseph Ferris
Are you just looking to verify that the file is of a given extension? You can simplify what you are trying to do with something like this:
您只是想验证文件是否具有给定的扩展名?您可以通过以下方式简化您尝试执行的操作:
(.*?)\.(jpg|gif|doc|pdf)$
Then, when you call IsMatch() make sure to pass RegexOptions.IgnoreCase as your second parameter. There is no reason to have to list out the variations for casing.
然后,当您调用 IsMatch() 时,请确保将 RegexOptions.IgnoreCase 作为您的第二个参数传递。没有理由必须列出外壳的变化。
Edit: As Dario mentions, this is not going to work for the RegularExpressionValidator, as it does not support casing options.
编辑:正如 Dario 所提到的,这不适用于 RegularExpressionValidator,因为它不支持大小写选项。
回答by PEZ
Your regexp seems to validate both the file name and the extension. Is that what you need? I'll assume it's just the extension and would use a regexp like this:
您的正则表达式似乎同时验证文件名和扩展名。那是你需要的吗?我会假设它只是扩展名,并会使用这样的正则表达式:
\.(jpg|gif|doc|pdf)$
And set the matching to be case insensitive.
并将匹配设置为不区分大小写。
回答by ICR
You can embed case insensitity into the regular expression like so:
您可以将大小写不敏感嵌入到正则表达式中,如下所示:
\.(?i:)(?:jpg|gif|doc|pdf)$
回答by mdunka
^.+\.(?:(?:[dD][oO][cC][xX]?)|(?:[pP][dD][fF]))$
Will accept .doc, .docx, .pdf files having a filename of at least one character:
将接受文件名至少为一个字符的 .doc、.docx、.pdf 文件:
^ = beginning of string
.+ = at least one character (any character)
\. = dot ('.')
(?:pattern) = match the pattern without storing the match)
[dD] = any character in the set ('d' or 'D')
[xX]? = any character in the set or none
('x' may be missing so 'doc' or 'docx' are both accepted)
| = either the previous or the next pattern
$ = end of matched string
Warning! Without enclosing the whole chain of extensions in (?:), an extension like .docpdf would pass.
警告!如果不将整个扩展链包含在 (?:) 中,像 .docpdf 这样的扩展将通过。
You can test regular expressions at http://www.regextester.com/
您可以在http://www.regextester.com/测试正则表达式
回答by Sajjad mc
You can use this template for every file type:
您可以将此模板用于每种文件类型:
ValidationExpression="^.+\.(([pP][dD][fF])|([jJ][pP][gG])|([pP][nN][gG])))$"
for ex: you can add ([rR][aA][rR]
) for Rar file type and etc ...
例如:您可以[rR][aA][rR]
为 Rar 文件类型等添加 ( ) ...