用于分析字符串url链接的正则表达式

本文关键字:链接 正则表达式 url 字符串 用于 | 更新日期: 2023-09-27 17:57:37

我正在寻找一种不使用System.Uri 将url链接解析为以下片段的方法

/默认.aspx/23/测试?var1=val1

我需要将这个url链接分解为值:

  1. 文件
  2. 路径信息
  3. 查询字符串

用于分析字符串url链接的正则表达式

这里有一个:

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

原始链接

这是我的代码:

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

这将生成命名组,检查您想要提取的