在ASP.net URL正前pression验证 [英] Regular expression validation for URL in ASP.net

查看:91
本文介绍了在ASP.net URL正前pression验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

([\\ w - ] +)+ [\\ w - ] +(/ [\\ W-./?%&=]*)?
我使用的URL验证上述前pression。但问题是,当我只给'www.yahoo'和preSS保存按钮,我没有得到任何错误消息。请提供一个解决方案。

感谢您


解决方案

好吧,我想这个职位涉及更多的蟒蛇。您可能会发现在ASP.NET以及类似的东西。

但是,如果你想自己解析它,你应该参考 RFC 2396(URI通用语法),它提供了正则表达式,并把它分成附录B部分:


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


  
  

在上面的第二线的数字仅协助
  可读性;它们指示各参照点
  SUBEX pression(即,每个成对括号)。我们指的是
  作为为$ SUBEX pression匹配的值。例如,匹配
  上述前pression到

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


  
  

结果如下SUBEX pression匹配:

  $ 1 = HTTP:
  $ 2 = HTTP
  $ 3 = //www.ics.uci.edu
  $ 4 = www.ics.uci.edu
  $ 5 = /酒吧/ IETF / URI /
  $ 6 =<&未定义GT;
  $ J =<&未定义GT;
  $ 8 = #Related
  $ 9 =相关


  
  其中,表示该组件是不是present,如


  对于在上述的例子的查询组件的情况。
  因此,我们可以确定四种成分的值,并
  片段作为

 方案= $ 16
  权威= $ 4个
  PATH = $ 5
  查询= $ 7
  片段= $ 9

和,在相反的方向前进,我们可以重新创建的URI引用
    从第5.2节的步骤7中使用的算法及其组件。


修改

有点谷歌搜索后,我发现这个在ASP.NET: 的System.Uri ,例如:

 开放的URI =新的URI(http://www.ics.uci.edu/pub/ietf/uri/#Related);
Console.WriteLine(uri.AbsoluteUri);
Console.WriteLine(uri.Host);

([\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

解决方案

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

这篇关于在ASP.net URL正前pression验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆