0

I have a large text from file (usually an html fine, but it can be any text). I need to replace a part of text with a string value.

string FullText = GetTextFromFile(filename);
string OldString = "<div class=\"main\">some text here</div>";
string NewString = "<div class=\"main replaced\" id=\"someid\">some new text here</div>";

Now I did a string replace

FullText=FullText.replace(OldString,NewString);

This does not always work. What I found the replace fails because the string which I need to find/replace, is not an exact match. Eg: in this case, the part of the fulltext might be

...<div class='main'>     some 
          text   here</div>...

note the single quotes instead of double quotes, and the number of spaces between the words. So, a basic string replace is not possible in this case.

My question: 1. is it possible to do a regex replace in this case? 2. if possible, what is the regex I need to use to get the correct sentence/part of the code?

In short, I need the regex replace to work irrespective of number of spaces/new line, or single/double quotes.

Toto
  • 89,455
  • 62
  • 89
  • 125
maX
  • 788
  • 2
  • 11
  • 32

1 Answers1

1

Try with

Regex.Replace(Your_html, @"<div\s+class\s*=\s*['|""]main['|""]\s*>((\r\n)*\s*\w+\s*(\r\n)*)+</div>","new text")
Fabio
  • 1,890
  • 1
  • 15
  • 19
  • thanks, this ((\r\n)*\s*\w+\s*(\r\n)*) would replace any sentence right? i dont want that to happen. There would be many such divs, and I only want to replace for -taking the example in question- the one which says "some text here". In that case should I insert he expression between every word? like "some((\r\n)*\s*\w+\s*(\r\n)*)+text((\r\n)*\s*\w+\s*(\r\n)*)+here" ? – maX Sep 22 '14 at 15:06
  • right, this part (\s*\w+\s*) must be with your sentence. You can use http://www.ultrapico.com/Expresso.htm for your test of the regex – Fabio Sep 22 '14 at 15:22
  • thanks, I havnt checked this yet, I will mark as answer once done. – maX Sep 24 '14 at 09:19