Reputation: 7774
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
Upvotes: 3
Views: 1808
Reputation: 64229
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, i.e.
/^(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 (i.e. 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, i.e \\a shouldn't be valid, neither should http://.somedomain.com
Result:
http://google.com: True
https://aa: True
\\a: True
\\\a: False
Upvotes: 1
Reputation: 151
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....
Upvotes: 0
Reputation: 9961
You are able to find a lot of regex expressions in library. E.g.: for your task.
Upvotes: 0