2011-05-26 25 views
2

我正在寻找一种方法来解析URL链接到以下部分无需使用System.Uri定期解析字符串URL链接表达

/Default.aspx/123/test?var1=val1

我要打破这个网址链接到值:

  1. 文件
  2. PATHINFO
  3. 查询字符串
+0

[url的正则表达式]的可能重复(http://stackoverflow.com/questions/833469/regular-expression-for-url) – Woot4Moo 2011-05-26 01:52:32

回答

1
string pattern= "\b(?<protocol>https?|ftp|gopher|telnet|file|notes|ms-help)://(?<domain>[-A-Z0-9.]+)(?<file>/[-A-Z0-9+&@#/%=~_|!:,.;]*)?(?<parameters>\?[-A-Z0-9+&@#/%=~_|!:,.;]*)?" 

这将生成命名组为您检查要提取

2

这里有一个:

string pattern = @"((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)" 

Origin Link

+1

这是一个不好的做法,只是发布链接。如果链接打破了你的回答,说明没有什么 – sra 2011-05-26 05:39:02

+1

够公平。但你可以通过使用“正则表达式url”来获得相同的答案,因为他们的工作似乎是错误的。 – bryanbcook 2011-05-27 04:21:20

1

这里什么是我的代码:

var match = Regex.Match(internalUrl, 
          @"^\/([\w|\/|\-|\,|\s]+)\.([a-zA-Z]{2,5})([\w|\/|\-|\,|\s]*)\??(.*)", 
          RegexOptions.IgnoreCase | RegexOptions.Singleline | 
          RegexOptions.CultureInvariant | RegexOptions.Compiled); 
    if (match.Success) 
    { 
     var filePath = match.Groups[1].Value; 
     var fileExtention = match.Groups[2].Value; 
     var pathInfo = match.Groups[3].Value; 
     var queryString = match.Groups[4].Value; 

     log.Debug("FilePath: " + filePath); 
     log.Debug("FileExtention: " + fileExtention); 
     log.Debug("PathInfo: " + pathInfo); 
     log.Debug("QueryString: " + queryString); 
    }