Click here to Skip to main content
14,775,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have this string :
<img  height="320" width="240" alt="" src="/Upload/userfiles/admin/6.JPG" />

<img width="240" height="320" alt="" src="/Upload/userfiles/admin/6.JPG" />

<img width="240" height="320" src="/Upload/userfiles/admin/6.JPG" />

<img  alt="" src="/Upload/userfiles/admin/6.JPG" />

i want replace
with for ex:
Updated 26-Jun-13 11:00am
Maciej Los 26-Jun-13 7:15am
And the problem is...
Anuja Pawar Indore 26-Jun-13 8:42am
Share what you are doing....your code

This is not a good idea to replace such strings using Regex, which is designed for some wide class of tricky searches.

Your strings are well-formed XML, so you could get to your ultimate goals faster, with more reliable results, if you leverage the power of existing XML parsers and generators. This is my short overview of them:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
  2. Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.

Good luck,
Maciej Los 26-Jun-13 18:01pm
I think it's possible to use RegEx class with RegEx.Replace()[^] method.
It should search for src=" and replace with: src="thumbnail.aspx?file=.
See my answer ;)
Sergey Alexandrovich Kryukov 26-Jun-13 19:15pm
Of course it's possible, but seems non-practical. If something is XML, is is usually treated better with XML software.
Maciej Los 27-Jun-13 2:18am
OK, i understand that technics based on XML are more practical, but OP question sounds: how to replace text using RegEx? I can't agree with this point of view that RegEx is not a good idea (...). In my opinion, in this case RegEx is a complete and useful tool.
Sergey, why do you claim that it's not practical? Can you explain it a bit more? I'm interested your opinion...
Best Regards, Maciej.
I see. Well, it's save to say "it depends" then. I think we discussed the matter.
Maciej Los 27-Jun-13 16:44pm
"It depends" closes the discussion ;)
Hope so.
Thank you, Maciej.
All you need to do is to get xml text into string variable and then to use RegEx.Replace()[^] method.

Please define pattern to search for src=" and replace with: src="thumbnail.aspx?file= ;)

How to get text from xml?
Xml file is - de facto - a text file (flat file), so you can use XMLTextReader[^] and Read()[^] method. After replacing text, use XMLTextWriter[^].

Reading XML with the XmlReader[^]
Writing XML with the XmlWriter[^]
lewax00 26-Jun-13 20:09pm
Assuming of course it is well formed XML, but not all HTML is. I think a plain TextReader/TextWriter would be more appropriate, if you don't plan on using related XML structures.
Maciej Los 27-Jun-13 1:39am
Thank you for your suggestion ;)
public static string OptimazeImages(string temp)

           int imgLoc = 0;
           int srcLoc = 0;
           int imgEndLoc = 0;

           while (imgLoc != -1)
               imgLoc = temp.IndexOf("<img", imgEndLoc);
               imgEndLoc = temp.IndexOf(">", imgLoc + 1);

               if (imgLoc == -1)
               srcLoc = temp.IndexOf("src=", imgLoc, imgEndLoc - imgLoc);

               temp = temp.Insert(srcLoc + 5, "ThumbnailViewer.aspx?X=0&Y=0&FilePath=");
           return temp;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900