-3

I read HTML file and do some stuff. After reading the file, I have to write a C# code to replace src values for img tag only. Can someone suggest a piece of code replace src values? Please find a sample HTML code which I have.

<!DOCTYPE html>
<html>
     <head>
     </head>
     <img alt="Mountain View" src="Tab1.png" style="width:304px;height:228px;">
     <img src="Tab2.png" alt="Mountain View" style="width:304px;height:228px;">
     <img alt="Mountain View" src="Tab3.png" style="width:304px;height:228px;">
     <img src="Tab4.png" alt="Mountain View" style="width:304px;height:228px;">
     <a href="https://www.w3schools.com">Visit W3Schools</a>
     </body>
</html>
Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
Sivakumar Piratheeban
  • 493
  • 4
  • 11
  • 39
  • 4
    [don't do that](https://stackoverflow.com/a/787987/5174469) ;) – Mong Zhu Jun 20 '17 at 11:12
  • 1
    As @MongZhu said... don't do that. Use the [HTML Agility Pack](https://stackoverflow.com/questions/787932/using-c-sharp-regular-expressions-to-remove-html-tags/787987#787987). It's what it's for. – Eric Burdo Jun 20 '17 at 12:06

1 Answers1

1

If you read the file line by line here is an example of how to replace the source with yours using regex:

string test = "<img alt=\"Mountain View\" src=\"Tab1.png\" style=\"width:304px;height:228px;\">";

string mySource = "src=\"Weird.png\" ";

string newtest = Regex.Replace(test, "src=\".+\"\\s",mySource); 

still I would suggest to follow the answer that I linked in my comment

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76