开发者

Regular expression validation for URL in ASP.net

([\w-]+.)+[\w-]+(/[\w- ./?%&=]*)? I am using the above expression for U开发者_StackOverflow社区RL 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


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜