myf*_*ums 6 c# regex asp.net validation file-upload
我试图详细了解这个RegEx声明.它应该从ASP.Net FileUpload控件验证文件名,只允许jpeg和gif文件.它是由其他人设计的,我不完全理解它.它在Internet Explorer 7.0中工作正常,但在Firefox 3.6中没有.
<asp:RegularExpressionValidator id="FileUpLoadValidator" runat="server"
ErrorMessage="Upload Jpegs and Gifs only."
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.gif|.GIF)$"
ControlToValidate="LogoFileUpload">
</asp:RegularExpressionValidator>
Run Code Online (Sandbox Code Playgroud)
这是一个简短的解释:
^ # match the beginning of the input
( # start capture group 1
( # start capture group 2
[a-zA-Z] # match any character from the set {'A'..'Z', 'a'..'z'}
: # match the character ':'
) # end capture group 2
| # OR
( # start capture group 3
\\{2} # match the character '\' and repeat it exactly 2 times
\w+ # match a word character: [a-zA-Z_0-9] and repeat it one or more times
) # end capture group 3
\$? # match the character '$' and match it once or none at all
) # end capture group 1
( # start capture group 4
\\ # match the character '\'
( # start capture group 5
\w # match a word character: [a-zA-Z_0-9]
[\w] # match any character from the set {'0'..'9', 'A'..'Z', '_', 'a'..'z'}
.* # match any character except line breaks and repeat it zero or more times
) # end capture group 5
) # end capture group 4
( # start capture group 6
. # match any character except line breaks
jpg # match the characters 'jpg'
| # OR
. # match any character except line breaks
JPG # match the characters 'JPG'
| # OR
. # match any character except line breaks
gif # match the characters 'gif'
| # OR
. # match any character except line breaks
GIF # match the characters 'GIF'
) # end capture group 6
$ # match the end of the input
Run Code Online (Sandbox Code Playgroud)
编辑
正如一些评论所要求的,上面是由我写的一个小工具生成的.你可以在这里下载:http://www.big-o.nl/apps/pcreparser/pcre/PCREParser.html(警告:正在开发中!)
编辑2
它将匹配这些字符串:
x:\abc\def\ghi.JPG
c:\foo\bar.gif
\\foo$\baz.jpg
Run Code Online (Sandbox Code Playgroud)
以下是第1组,第4组和第6组分别匹配的内容:
group 1 | group 4 | group 6
--------+--------------+--------
| |
x: | \abc\def\ghi | .JPG
| |
c: | \foo\bar | .gif
| |
\\foo$ | \baz | .jpg
| |
Run Code Online (Sandbox Code Playgroud)
请注意,它也匹配字符串,c:\foo\bar@gif
因为DOT匹配任何字符(换行符除外).它会拒绝像一个字符串c:\foo\bar.Gif
(大写G
的gif
).
这是一个糟糕的正则表达式。
^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.gif|.GIF)$
Run Code Online (Sandbox Code Playgroud)
让我们一点一点地做。
([a-zA-Z]:)
Run Code Online (Sandbox Code Playgroud)
这要求文件路径以驱动器号开头C:
,例如d:
、 等。
(\\{2}\w+)\$?)
Run Code Online (Sandbox Code Playgroud)
\\{2}
表示反斜杠重复两次(注意\
需要转义),后跟一些字母数字 ( \w+
),然后可能是美元符号 ( \$?
)。这是 UNC 路径的主机部分。
([a-zA-Z]:)|(\\{2}\w+)\$?)
Run Code Online (Sandbox Code Playgroud)
意思|
是“或”。因此要么以驱动器号或 UNC 路径开头。恭喜您踢出了非 Windows 用户。
(\\(\w[\w].*))
Run Code Online (Sandbox Code Playgroud)
这应该是路径的目录部分,但实际上是 2 个字母数字,后跟除换行符 ( .*
) 之外的任何内容,例如\ab!@#*(#$*)
.
这部分的正确正则表达式应该是(?:\\\w+)+
(.jpg|.JPG|.gif|.GIF)$
Run Code Online (Sandbox Code Playgroud)
这意味着路径的最后 3 个字符必须是jpg
、JPG
或gif
。GIF
请注意,.
不是点,而是匹配除 之外的任何内容,因此类似或 的\n
文件名将通过。haha.abcgif
malicious.exe\0gif
这部分的正确正则表达式应该是\.(?:jpg|JPG|gif|GIF)$
一起,
^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.gif|.GIF)$
Run Code Online (Sandbox Code Playgroud)
将匹配
D:\foo.jpg
\\remote$\dummy\..\C:\Windows\System32\Logo.gif
C:\Windows\System32\cmd.exe;--gif
Run Code Online (Sandbox Code Playgroud)
并将失败
/home/user/pictures/myself.jpg
C:\a.jpg
C:\d\e.jpg
Run Code Online (Sandbox Code Playgroud)
正确的正则表达式是,并在服务器端/\.(?:jpg|gif)$/i
检查上传的文件是否确实是图像。
归档时间: |
|
查看次数: |
560 次 |
最近记录: |