这场比赛的Regex

本文关键字:Regex | 更新日期: 2023-09-27 18:25:16

所以我有示例url http://citrine.rcd.dev:8080/image/1582_125x125.jpg

它是用代码构建的:

    public string GetDerivativeUrl(long imageId, bool preserveTransparency, DerivativeSize size, bool allowCache, string designTags, int quality)
    {
        string tags = (designTags == null || designTags.Trim().Length == 0) ? "" : designTags;
        string imageFormat = (preserveTransparency) ? "png" : "jpg";
        if (quality > 0)
            return String.Format("{0}/image/{4}{1}_{2}.{3}?qv={5}", GetUrlPrefix(imageId, allowCache), imageId, size.GetPixelSize(), imageFormat, tags, quality);
        return String.Format("{0}/image/{4}{1}_{2}.{3}", GetUrlPrefix(imageId, allowCache), imageId, size.GetPixelSize(), imageFormat, tags);
    }

我想从http://citrine.rcd.dev:8080/image/1582_125x125.jpg 获得http://citrine.rcd.dev:8080/image/1582.jpg

我该怎么做?

这场比赛的Regex

使用System.Uri从路径中获取文件名部分,然后用"." 替换"_.*?'."

您可以这样做,而不是使用Regex。

string url = @"http://citrine.rcd.dev:8080/image/1582_125x125.jpg";
int index = url.LastIndexOf('_');
if (index != -1)
   url = url.Substring(0, index) + Path.GetExtension(url);