Ivan Prodanov
Ivan Prodanov

Reputation: 35502

Regex for this match

So I have sample url http://citrine.rcd.dev:8080/image/1582_125x125.jpg

It's built with the code:

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

I want to get http://citrine.rcd.dev:8080/image/1582.jpg from http://citrine.rcd.dev:8080/image/1582_125x125.jpg

How do I do this?

Upvotes: 2

Views: 64

Answers (2)

keyboardP
keyboardP

Reputation: 69372

You could do this instead of using a 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);

Upvotes: 1

sehe
sehe

Reputation: 392903

Use System.Uri to get the filename part from the path and then replace "_.*?\." by "."

Upvotes: 1

Related Questions