Replace text with RegEx and C # does not work as I need


I’m looking for a way to go through a string and replace all instances where the second and third characters will always be different but the rest will be the same. For example, if I had:

"ú07ú" to be replaced with "ú07 ú"
"ú1Eú" to be replaced with "ú1E ú"
"ú12ú" to be replaced with "ú12 ú"

I know I should use Regular Expressions, but they baffle me. I’m pretty sure the syntax will be something like:

Content = Regex.Replace(Content, @"ú...", “ú.. ú");

But obviously this isn’t working. Can any RegEx gurus lend a hand please?


Looks like you want:

Content = Regex.Replace(Content, @"ú([^ú]+)ú", @"ú$1 ú");

This regex:


Means: match ú, then at least one character that isn't ú (and capture this part), then another ú. If you want it to only match exactly two characters in the middle, then change [^ú]+ to [^ú]{2}

Then we replace the whole thing by:

ú$1 ú

Which is: ú, then the captured part of the string, then a space and ú again.