1

A URL should start with http:// or www.

http://google.com

or

www.google.com

My Regular expression is:

 [RegularExpression(@"^http(s?)\:\/\/[0-9a-zA-Z]([.\w][0-9a-zA-Z])(:(0-9))(\/?)([a-zA-Z0-9\\.\?\,\'\/\\\+&%\$#_])?$", ErrorMessage = "*")]

My code is working fine for http://google.com but not for www.google.com.

Does anyone have any suggestions?

Greg
  • 481
  • 1
  • 5
  • 21
Ram
  • 337
  • 5
  • 23
  • possible duplicate - http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url – rs. Dec 28 '12 at 11:52
  • what infos do you want to extract from the url? – VladL Dec 28 '12 at 12:03

1 Answers1

0

Try this rule:

'#((https?://|www\.)([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)#'
Klevis Miho
  • 851
  • 8
  • 15
  • I like it except the amount of capturing it does, make some (most) groups non-capturing and you'll get my +1 – Paul S. Dec 28 '12 at 11:56
  • It's been a long time since I have dealt with regexes. This I had posted on my blog a long time ago. – Klevis Miho Dec 28 '12 at 12:00