Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于特定扩展验证的正则表达式_C#_Regex_Asp.net Core Mvc_Entity Framework Core_Data Annotations - Fatal编程技术网

C# 用于特定扩展验证的正则表达式

C# 用于特定扩展验证的正则表达式,c#,regex,asp.net-core-mvc,entity-framework-core,data-annotations,C#,Regex,Asp.net Core Mvc,Entity Framework Core,Data Annotations,我试图用DataAnnotation验证客户端(razor)上传的文件扩展名,但是看起来无论上传什么文件,我总是会收到“错误消息” 我的代码如下: <input name="MyImage" class="input-validation-error" id="MyImage" aria-invalid="true" aria-describedby="MyImage-error" type="file" data-val="true" data-val-regex-pattern="^

我试图用DataAnnotation验证客户端(razor)上传的文件扩展名,但是看起来无论上传什么文件,我总是会收到“错误消息”

我的代码如下:

 <input name="MyImage" class="input-validation-error" id="MyImage" aria-invalid="true" aria-describedby="MyImage-error" type="file" data-val="true" data-val-regex-pattern="^.*\.(jpg | JPG | gif | docx | doc | DOC | pdf | PDF)$" data-val-regex="bla bla">

您的原始表达式工作正常,让我们使用
i
标志稍微简化它:

(.*\.)(jpe?g|gif|docx?|pdf)$
试验 正则表达式电路 可视化正则表达式:


您的原始表达式工作正常,让我们使用
i
标志稍微简化它:

(.*\.)(jpe?g|gif|docx?|pdf)$
试验 正则表达式电路 可视化正则表达式:


如果不想匹配空格,请不要添加空格。@WiktorStribiżew我不确定我是否理解。这是我想要验证的唯一扩展,所有其他扩展都假设得到错误消息。
@“^.*”(jpg | jpg | gif | docx | doc | doc | pdf |)$“
[正则表达式(@“^.*”(jpg | jpg | gif | docx | docx | doc doc | pdf)$”,我仍然收到错误为什么您在
jpg
之后插入空格?这是使用我的建议的唯一问题吗?如果您不想匹配空格,请不要添加空格。@WiktorStribiżew我不确定我是否理解。这是我要验证的唯一扩展,其他所有扩展都应该得到错误消息。
@^.*\。(jpg | jpg | gif | docx | doc | doc | doc | pdf | pdf)$“
[RegularExpression(@“^...*”(jpg | jpg | gif | docx | doc docx | doc doc pdf)$”,我仍然收到错误为什么在
jpg
之后插入空格?这是使用我的建议的唯一问题吗?
using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"(.*\.)(jpe?g|gif|docx?|pdf)$";
        string input = @"any_file_name_we_wish_here_with_!@#$%^&*_1234567.jpg
any_file_name_we_wish_here_with_!@#$%^&*_1234567.jpeg
any_file_name_we_wish_here_with_!@#$%^&*_1234567.JPG
any_file_name_we_wish_here_with_!@#$%^&*_1234567.docx
any_file_name_we_wish_here_with_!@#$%^&*_1234567.doc
any_file_name_we_wish_here_with_!@#$%^&*_1234567.pdf
any_file_name_we_wish_here_with_!@#$%^&*_1234567.gif
any_file_name_we_wish_here_with_!@#$%^&*_1234567.mp3
any_file_name_we_wish_here_with_!@#$%^&*_1234567.mp4";
        RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnoreCase;

        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}