我有多个图像的降价内容,其中每个图像都是:
![Image Description](/image-path.png)
我正在使用Regex:选择降价内容中的所有图像
var matches = new Regex(@"![.*?]((.*?))").Matches(content);
有了这个,我每场比赛都有两组:
Groups[0] = ![Image Description](/image-path.png); > (Everything)
Groups[1] = /image-path.png > (Image Path)
对于`content中的每个图像路径,都需要用新的Guid替换它。
然后将每个Guid和映像路径添加到以下字典中。
Dictionary<String, String> imagePathsKeys = new Dictionary<String, String>();
我试图使用Regex.Replace
,但我找不到一种方法来只将"![.*?]((.*?))"
中的图像路径替换为Guid,提取旧值并将其作为Key和value添加到字典中。
如果我理解正确,这就是实现这一目标的方法。Dotnet小提琴。
Dictionary<String, String> imagePathsKeys = new Dictionary<String, String>();
var content = "![First Image Description](/image-path.png) ![Second Image Description](/image-path.png)";
// This regex will only match whatever is between / and .
// You may have to play around with it so it finds image names more accurately in your actual project
var matches = new Regex(@"(?<=/)(.*?)(?=.)").Matches(content);
foreach (Match match in matches)
{
GroupCollection groups = match.Groups;
var guid = Guid.NewGuid().ToString(); // generating new guid
imagePathsKeys.Add(guid, groups[0].Value); // adding new guid as key and image path as value
content = content.Replace(groups[0].Value, guid); // replacing image path with new guid
}