代码之家  ›  专栏  ›  技术社区  ›  cyberpirate92

替换字符串中与模式匹配的部分

  •  1
  • cyberpirate92  · 技术社区  · 6 年前

    我正在尝试删除URL字符串的一部分(协议、查询字符串等)

    https://www.example.com/xyz/page.html?id=10&name=smith
    http://www.example.com/abc/index.html#
    https://www.example.com/abc/
    www.example.com/abc
    example.com/abc
    http://example.com/abc
    

    将成为

    example.com/xyz/page.html
    example.com/abc/index.html
    example.com/abc
    example.com/abc
    example.com/abc
    example.com/abc
    

    这就是我目前所做的,

    string CleanUrl(string urlString)
    {
            urlString = Regex.Replace(urlString, @"^https?://", "", RegexOptions.IgnoreCase);
            urlString = Regex.Replace(urlString, @"^www\.", "", RegexOptions.IgnoreCase);
            urlString = Regex.Replace(urlString, @"#$", "");
            urlString = Regex.Replace(urlString, @"/$", "");
            return urlString;
    }
    

    我在找一个更好的方法,也许用一个 Regex.Replace

    编辑: 对不起,我的问题不清楚。我的输入字符串有时不包含协议和/或 www. 部分,导致 System.UriFormatException Uri(urlString) 建造师。我已经更新了示例输入。

    4 回复  |  直到 6 年前
        1
  •  0
  •   The fourth bird    6 年前

    如果您的所有字符串都是url,并且您不必验证该结构,那么对于示例数据,您可以使用替换项来匹配您要从url中删除的内容,并替换为空字符串。

    ^(?:https?://www\.|https?://|www\.)?|(?:[#/]|\?.*)$

    解释

    • ^(?:https?://www\.|https?://|www\.)? 断言字符串的开头,后跟可选的非捕获组,该组将匹配http和可选的s,后跟://www.或仅匹配http://part或仅匹配www.part。
    • | 或者
    • (?:[#/]|\?.*)$ 匹配下列任一项 #/

    Regex demo

    C# demo

        2
  •  1
  •   fasaas    6 年前

    我会用我在问题中的评论。

       public string ReplaceUrl(string input)
        {
            Uri uri = new Uri(input);
    
            string uriWithoutQueryParams = uri.GetLeftPart(UriPartial.Path);
    
            string uriWithoutSchema = uriWithoutQueryParams.Replace(uri.GetLeftPart(UriPartial.Scheme), string.Empty);
    
            string uriWithoutTripleW = uriWithoutSchema.Replace("www.", string.Empty);
    
            string uriWithoutTrailingSlash = uriWithoutTripleW.TrimEnd(new char[] {'/' });
    
            return uriWithoutTrailingSlash;
        }
    

    下面是您需要的测试方法(使用XUnit)

        [Theory]
        [InlineData("https://www.example.com/xyz/page.html?id=10&name=smith", "example.com/xyz/page.html")]
        [InlineData("http://www.example.com/abc/index.html#", "example.com/abc/index.html")]
        [InlineData("https://www.example.com/abc/", "example.com/abc")]
        public void MyUrlConverterReplacesCorrectly(string inputUrl, string expectedUrl)
        {
            string actualUrl = MyUrlConverter.ReplaceUrl(inputUrl);
    
            Assert.Equal(expectedUrl, actualUrl);
        }
    
        3
  •  0
  •   adjan    6 年前

    Uri 类来解析URL字符串,然后使用 Host AbsolutePath 属性以获取最终字符串:

    var uri = new Uri("https://www.example.com/xyz/page.html?id=10&name=smith");
    var result = uri.Host + uri.AbsolutePath;
    if (result.EndsWith("/"))
        result = result.Remove(result.Length - 1, 1);
    if (result.StartsWith("www."))
        result = result.Substring(4);
    
        4
  •  0
  •   protoproto    6 年前

    试试这个:

            static string CleanUrl(string urlString)
            {
                urlString = Regex.Replace(urlString, @"\s+", "");
                urlString = Regex.Replace(urlString, @"^https?://", "", RegexOptions.IgnoreCase);
                urlString = Regex.Replace(urlString, @"^www\.", "", RegexOptions.IgnoreCase);
                urlString = Regex.Replace(urlString, @"(#|\?).*$", "");
                urlString = Regex.Replace(urlString, @"/$", "");
                return urlString;
            }