Replacing text, but I need to maintain the case

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>

System.Text.RegularExpressions.Regex.Replace(strOriginal, “(?<=>.)color(?=.</)”, " colour")

Regex i use is (?<=>.)color(?=.</)

Thank You,

Hi,

How about the following expression?

System.Text.RegularExpressions.Regex.Replace(strData,"(?<=(?<!"")[Cc]olo)(?=r(?!""))","u")

Regards,

1 Like

The data is not as dynamic, its not color always, it could be any word.
In that case whats the regex to use

|aluminum|aluminum|

|analog|analogue|
|analyze|analyze|
|analyzes|analyzes|

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.

But if i have a word within < h1 class=“color”> I don’t want to change. I just want to change highlighted part

<li class="color"> ***Hello my favourite color is Red Color***</li>

Thanks, Got it

How about using below logic:
dt_Mapping will look like below
image

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>

1 Like

This one works!!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.