简体   繁体   English

通过正则表达式验证文件类型

[英]Validating file types by regular expression

I have a .NET webform that has a file upload control that is tied to a regular expression validator. 我有一个.NET webform,它有一个与正则表达式验证器绑定的文件上传控件。 This validator needs to validate that only certain filetypes should be allowed for upload (jpg,gif,doc,pdf) 此验证程序需要验证是否只允许上传某些文件类型(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? 然而,这似乎并没有起作用...任何人都可以给我一点注册帮助吗?

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 Expresso to test your regular expressions. 您可以使用Expresso之类的工具来测试正则表达式。

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. 然后,当您调用IsMatch()时,请确保将RegexOptions.IgnoreCase作为第二个参数传递。 There is no reason to have to list out the variations for casing. 没有理由要列出套管的变化。

Edit: As Dario mentions, this is not going to work for the RegularExpressionValidator, as it does not support casing options. 编辑:正如Dario所提到的,这不适用于RegularExpressionValidator,因为它不支持套管选项。

^.+\.(?:(?:[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/上测试正则表达式

您可以将case insensitity嵌入到正则表达式中,如下所示:

\.(?i:)(?:jpg|gif|doc|pdf)$

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 ... 例如:您可以为Rar文件类型等添加( [rR][aA][rR] )...

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. 并将匹配设置为不区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM