简体   繁体   中英

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 \\\\

I'm using 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 \\\\

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 \\\\

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. 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

Update 2: It is a common error, and / doesn't have to be escaped in C# neither, it is the perl syntax. In perl (and PHP which uses perl regex estension, all the preg_xxx methods), one has to set a delimiter. 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 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

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

Result:

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

They didn't fail for me. I checked it with RegEx.IsMatch().

But if you want to check for a double \\ at the beginning your regex should be ^(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:

    /// <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....

You are able to find a lot of regex expressions in library . Eg: for your task .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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