1

([\w-]+.)+[\w-]+(/[\w- ./?%&=]*)? I am using the above expression for URL validation. But the problem is when I give only 'www.yahoo' and press the save button I didn't get any error message. Please offer a solution.

Thank you

Binoy
  • 151
  • 1
  • 14
  • here's (somewhat) duplicate post: http://stackoverflow.com/questions/827557/how-do-you-validate-a-url-with-a-regular-expression-in-python – William Niu Jul 23 '11 at 01:15

1 Answers1

3

Well, I guess that post relates more to python. You might find something similar in ASP.NET as well.

But if you want to parse it yourself, you should refer to the RFC 2396 (URI Generic Syntax), which provides the regex and breaks it into components in Appendix B:

  ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
   12            3  4          5       6  7        8 9

The numbers in the second line above are only to assist readability; they indicate the reference points for each subexpression (i.e., each paired parenthesis). We refer to the value matched for subexpression as $. For example, matching the above expression to

  http://www.ics.uci.edu/pub/ietf/uri/#Related

results in the following subexpression matches:

  $1 = http:
  $2 = http
  $3 = //www.ics.uci.edu
  $4 = www.ics.uci.edu
  $5 = /pub/ietf/uri/
  $6 = <undefined>
  $7 = <undefined>
  $8 = #Related
  $9 = Related

where indicates that the component is not present, as is the case for the query component in the above example. Therefore, we can determine the value of the four components and fragment as

  scheme    = $2
  authority = $4
  path      = $5
  query     = $7
  fragment  = $9

and, going in the opposite direction, we can recreate a URI reference from its components using the algorithm in step 7 of Section 5.2.

EDIT:

After a bit of Googling, I found this in ASP.NET: System.Uri, e.g.:

Uri uri = new Uri("http://www.ics.uci.edu/pub/ietf/uri/#Related");
Console.WriteLine(uri.AbsoluteUri);
Console.WriteLine(uri.Host);
William Niu
  • 15,798
  • 7
  • 53
  • 93