Regular Expression to parse string url links

房东的猫 提交于 2019-12-22 12:27:45

问题


I am looking for a way to parse url link into following segments without using System.Uri

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

I need to break down this url link into values:

  1. File
  2. PathInfo
  3. Querystring

回答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+&@#/%=~_|!:,.;]*)?"

This will generate named groups check for for what you want to extract




回答2:


Here's one:

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

Origin Link




回答3:


Here is my code:

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


来源:https://stackoverflow.com/questions/6132693/regular-expression-to-parse-string-url-links

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!