简体   繁体   English

用于检查url和共享文件路径的正则表达式

[英]Regular expression to check url and shared file path

I'm using below regular expression for Dataform field for checking whether the entered text starts with http:// or https:// or \\\\ 我正在使用Dataform字段的正则表达式下面检查输入的文本是以http://https://还是\\\\开头

I'm using System.componentmodel.DataAnnotations.RegularExpressionAttribute 我正在使用System.componentmodel.DataAnnotations.RegularExpressionAttribute

 [Display(Name = "URL", Description = "URL")]
 [RegularExpression(@"^(http[s]{0,1}:\/\/|\\\\)", 
 ErrorMessage = "Please enter valid Url or filepath")]
 public string URL { get; set; }

but in dataform field it is throwing error if any text is enetered after http:// or https:// or \\\\ 但是在dataform字段中,如果在http://https://\\\\之后存在任何文本,则会抛出错误

http://google.com     ---failed
https://aa        --failed
\\a         ----failed

I just want to pass all the above scenarios ...on high level the regular expression should just only check whether entered text starts with http:// or https:// or \\\\ 我只想传递上述所有场景......在高级别上,正则表达式应仅检查输入的文本是以http://https://还是\\\\ 开头

And even dataform is throwing error for the field when user enters and delete the text and click on tab.the error is URL is required field , but I didn't mention required attribute for this property. 甚至当用户输入和删除文本并单击选项卡时,数据形式也会为该字段抛出错误。错误是URL是必填字段 ,但我没有提到此属性的必需属性。 Please help 请帮忙

You are using a literal string, but you're trying to escape it. 您正在使用文字字符串,但您正试图逃避它。

[RegularExpression(@"^(http[s]{0,1}:\/\/|\\\\)", 

A literal string starts with @"..." and don't need to be escaped. 文字字符串以@"..."开头,不需要转义。 So either use 所以要么使用

[RegularExpression("^(http[s]{0,1}://|[\\\\]{2})(?:[\\w][\\w.-]?)", 

or 要么

[RegularExpression(@"^(http[s]{0,1}://|[\\]{2})(?:[\w][\w.-]?)", 

Update: You can also read more about string literals on MSDN: String Literals 更新:您还可以在MSDN上阅读有关字符串文字的更多信息: String Literals

Update 2: It is a common error, and / doesn't have to be escaped in C# neither, it is the perl syntax. 更新2:这是一个常见错误,并且/不必在C#中进行转义,它是perl语法。 In perl (and PHP which uses perl regex estension, all the preg_xxx methods), one has to set a delimiter. 在perl(以及使用perl regex estension,所有preg_xxx方法的PHP)中,必须设置分隔符。 In this languages the regex will begin with a delimiter, which is an symbol that shows the begin and the end of the regex pattern, ie 在这种语言中,正则表达式将以分隔符开始,分隔符是显示正则表达式模式的开始和结束的符号,即

/^(http[s]?:\\/\\/... /i The first / is a delimiter, that's why // from http:// has to be escaped. /^(http[s]?:\\/\\/... /i第一个/是分隔符,这就是为什么//来自http://必须进行转义。

#^(http[s]?://... #i The first # is now the delimiter, that's why // from http:// doesn't have to be escaped. The instruction after the delimiter (ie i in this case) just tells to do a case insensitive match #^(http[s]?://... #i第一个#现在是分隔符,这就是为什么//从http://不必转义。分隔符后的指令(即我在这种情况)只是告诉做一个不区分大小写的匹配

Some example I used to test it: 我用来测试它的一些例子:

        string[] inputs = { @"http://google.com", @"https://aa", @"\\a", @"\\\a" };
        Regex regEx = new Regex(@"^(http[s]{0,1}://|[\\]{2})(?:[\w][\w.-]?)+", RegexOptions.Compiled);
        foreach(var input in inputs) {
            var match = regEx.Match(input);
            Console.WriteLine(string.Format("{0}:\t{1} => {2}", input, match.Success, match.Success?match.Groups[1].Value:string.Empty));
        }

The (?:[\\w][\\w.-]?)+ at the end is to make sure, it that a word followed by a words, ie \\\\a shouldn't be valid, neither should http://.somedomain.com 最后的(?:[\\w][\\w.-]?)+是为了确保一个单词后跟一个单词,即\\\\ a应该无效, http://.somedomain.com也不应该http://.somedomain.com

Result: 结果:

http://google.com:      True
https://aa:     True
\\a:    True
\\\a:   False

They didn't fail for me. 他们没有为我失败。 I checked it with RegEx.IsMatch(). 我用RegEx.IsMatch()检查了它。

But if you want to check for a double \\ at the beginning your regex should be ^(http[s]{0,1}://|\\\\) 但是如果你想在开头检查一个双\\ \\你的正则表达式应该是^(http [s] {0,1}:// | \\\\)

You can try 你可以试试

^(http[s]?://|\\\\) 

which is actually equal to your Regex. 这实际上等于你的正则表达式。

When you want the regex to be capital insensitive use: 当您希望正则表达式对资本不敏感时使用:

(?i)^(http[s]?://|\\\\)

or 要么

(?i)^(http[s]?://|\\\\).+$

Addition: 加成:

I have tried this in my MVC 4 application and it works: 我在我的MVC 4应用程序中尝试了这个并且它可以工作:

    /// <summary>
    /// Gets or sets the website.
    /// </summary>
    [RegularExpression(@"(?i)^(http[s]?://|\\\\).+$", ErrorMessage = "Not good website")]
    public string Website { get; set; }

Isn't your input somehow scrumbled. 不是你的输入不知何故。 I know PHP has a feature that adds magic quotes.... 我知道PHP有一个添加魔术引号的功能....

You are able to find a lot of regex expressions in library . 你可以在库中找到很多正则表达式。 Eg: for your task . 例如: 为了你的任务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM