0

Regular expression for “Url Cannot start with www in prefix and white spaces" please help me frnds.

eg:goole.com is correct

eg:www.google.com is not accepted

eg: www.google.com is not accepted

prefix not www. and not start with whitespace -------->show fail

googlewwwwwwwwww.com ---->show success

Thanks in advance...

user1858830
  • 1
  • 1
  • 2
  • 1
    should see this, might help you http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address http://stackoverflow.com/questions/2063213/regular-expression-for-validating-dns-label-host-name – samaniego Nov 28 '12 at 06:46

1 Answers1

0

Here is a non-regex solution.

string str = "goole.com";
Uri tempUri;
if (!str.StartsWith(" ") //shouldn't start with space
    && !str.StartsWith("www") //shoudn't start with www
    && Uri.TryCreate(str, UriKind.RelativeOrAbsolute, out tempUri) //should be 
                                                                   //a valid Uri
    )
{
    //valid url
}
else
{
    //invalid url
}
Habib
  • 205,061
  • 27
  • 376
  • 407
  • hi habib i am using regular expressions prefix should not www. and whitespace so help me and using regular expressions – user1858830 Nov 28 '12 at 10:34
  • @user1858830, I am no expert with regex, why can't you use this check in your code ? – Habib Nov 30 '12 at 11:13