I have a string, the text only within the HTML tags should be replaced, ‘Color’ But needs to be ‘Colour’ and ‘color’ needs to be replaced with ‘colour’ .Just holding the case it has.
But I do these changes based on the lookup table
color - colour
I need to maintain the case as it is.
Input
<li>Hello my favourite color is Red Color</li>
<li class="color"> Hello my favourite color is Red Color</li>
Output
<li>Hello my favourite colour is Red Colour.</li>
<li class="color"> Hello my favourite colour is Red Colour.</li>
You can use the Replace method. It is case-sensitive in nature.
e.g. Log Message = "Hello my favourite color is Red Color".Replace("color","colour") Output>> "Hello my favourite colour is Red Color"
You can create a loop on your lookup table for replacement.
How about using below logic: dt_Mapping will look like below
For each row as DataRow in dt_Mapping.Rows
str_Input = Regex.Replace(str_Input,"(?!\>[\w ]*)" & CurrentRow("Column1").ToString & "(?=[\w ]*\<)",CurrentRow("Column2").ToString)
Next row
Sample Input <li>Hello my favourite color is Red Color</li><li class="color"> Hello my favourite color is Red Color</li>
Output <li>Hello my favourite colour is Red Color</li><li class="color"> Hello my favourite colour is Red Color</li>