1

What would be the regular expression to find the substrings www. and http:// in a domain name.

Basically I am writing an MVC validation to disallow a word that includes www., http:// or http://www. in it.

1 Answer 1

3

Try:

Regex.IsMatch(myString, @"(www\.|http://)");

If you want to specify that they aren't allowed at the start of the string, but allow them elsewhere, use:

Regex.IsMatch(myString, @"^(www\.|http://)");
Sign up to request clarification or add additional context in comments.

2 Comments

I plugged it into the validation and it is returning true no matter what. [RegularExpression(@"(www\.|http://)",ErrorMessage="http:// or www. is not permitted the domain name.")] Any idea? Thanks!
This is because you need to then assert in your validation that matches are invalid but the Regex DataAnnotation asserts that matches are valid. Regex is not good at checking that a string "doesn't have a substring" (without horrible lookarounds), so perhaps you could look into implementing a custom validator for DoesNotMatch? msdn.microsoft.com/en-us/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.