gpt4 book ai didi

c# - 理解这个 RegEx 语句

转载 作者:太空狗 更新时间:2023-10-29 21:00:11 26 4
gpt4 key购买 nike

我正在尝试详细了解此 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>

最佳答案

这里有一个简短的解释:

^               # 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

编辑

根据一些评论的要求,以上是我写的一个小工具生成的。您可以在这里下载:http://www.big-o.nl/apps/pcreparser/pcre/PCREParser.html (警告:大量开发中!)

编辑 2

它将匹配如下字符串:

x:\abc\def\ghi.JPG
c:\foo\bar.gif
\\foo$\baz.jpg

以下是第 1、4 和 6 组分别匹配的内容:

group 1 | group 4      | group 6
--------+--------------+--------
| |
x: | \abc\def\ghi | .JPG
| |
c: | \foo\bar | .gif
| |
\\foo$ | \baz | .jpg
| |

请注意,它也匹配 c:\foo\bar@gif 这样的字符串,因为 DOT 匹配任何字符(换行符除外)。它会拒绝像 c:\foo\bar.Gif 这样的字符串(gif 中的大写 G)。

关于c# - 理解这个 RegEx 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2289605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com