c# - 理解这个 RegEx 语句

标签 c# asp.net regex file-upload validation

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

相关文章:

c# - 如何在仅 EF4.1 代码场景中重用部分 Linq to Entity Select 表达式

c# - 使用 DateTime.Now 有什么问题。作为唯一 ID 的主要部分?

c# - 将通用参数绑定(bind)到 Web API 中的自定义模型绑定(bind)器

c# - 使用 Linq to Entities 插入现有行的副本

c# - 如何在 JavaScript 方法中获取 ASP.NET DataGrid 控件的选定行索引?

c# - MVC 4 - Razor - 将变量传递到 href url

c# - C# 中的 PayPal IPN 监听器

php - 在哪里可以找到完整的正则表达式引用?

JavaScript 正则表达式 : Replace curly braces

ios - 将字符串中匹配的正则表达式值替换为字典中的正确值