2013-07-29 72 views
2

所以我有样品网址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我该怎么办呢?

+0

有什么不对的回报'的String.Format( “{0} /图像/ {3} {1} {2}”,GetUrlPrefix(图像标识,allowCache),图像标识,的imageformat,标签);'? – sehe

+0

那么你想要在这个方法中创建字符串后更改字符串,还是要更改该方法,以便它返回所需的字符串? – sloth

回答

1

使用System.Uri得到的文件名部分从路径,然后替换"_.*?\."通过"."

1

你可以这样做,而不是使用一个正则表达式这一点。

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