定期EX pression验证文件类型 [英] Validating file types by regular expression
问题描述
我有一个.NET Web窗体,有一个文件上传控件绑定到一个普通的前pression验证。这个验证需要验证,只有某些文件类型应该被允许上传(JPG,GIF,DOC,PDF格式)
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)
目前常规EX pression,这是否是:
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)$
您可以像使用前preSSO 一个工具来测试你的正常EX pressions。
You can use a tool like Expresso to test your regular expressions.
这篇关于定期EX pression验证文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!