Glinkot
Glinkot

Reputation: 2964

Regex find and replace (c# .net)

I have a long HTML page as a string. Throughout, I have image tags with text like this:

cid:@(Model.MyImage)

I have strings storing the path and file extension. I'd like to replace the above to get

[mypath]MyImage.[myExtension]

So I'd like to capture that MyImage text and use it in my new string - then get rid of all the cid: stuff and put the standard path in.

So, cid:@(Model.MainChart) might become C:\MainChart.png

EDIT

Per the answer below, the question has been answered nicely. Example of usage below:

string first = @"<img src=""cid:@(Model.FirstOne)"" style=""max-width:600px;"" then <img src=""cid:@(Model.Another)"" style=""max-width:600px;"")";
            string pathPrefix = @"C:\";
            string extension = ".png";
            string newPath = Regex.Replace(first, @"cid:@\(Model\.([^)]*)\)", pathPrefix + "$1" + extension);

Upvotes: 1

Views: 668

Answers (1)

Fischermaen
Fischermaen

Reputation: 12458

Here is my suggestion for you. The Regex is:

cid:@\(Model\.([^)]*)\)

and the replacement string should contain a $1 for the found image.

Upvotes: 3

Related Questions