Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# How to replace an accent insensitive string with regex?

Tags:

c#

regex

I'd like to perform an accent-insensitive replace in a string. I want 'client' to match 'cliënt' and vice versa.

My code looks like this:

Regex reg = new Regex("client");
string result = reg.Replace("here goes the content with client and cliënt", "replacementWith");

So, how do I make sure that 'client' matches 'client' and 'cliënt' and vice versa?

like image 806
Martijn Avatar asked May 16 '11 13:05

Martijn


1 Answers

You can include it in the Regex

Regex reg = new Regex("cli[eë]nt"); // will match both 'client' and 'cliënt' 

or you can remove all the accents in the string and then apply the regular expression.

string test = "here góes the cóntent with client and cliënt";

char[] replacement = { 'a','a','a','a','a','a','c','e','e','e','e','i','i','i','i','n','o','o','o','o','o','u','u','u','u','y','y' };
char[] accents = { 'à','á','â','ã','ä','å','ç','é','è','ê','ë','ì','í','î','ï','ñ','ò','ó','ô','ö','õ','ù','ú','û','ü','ý','ÿ' };


for (int i = 0; i < accents.Length; i++)
{
    test = test.Replace(accents[i], replacement[i]);
}

This is not very efficient but will do the job for small amounts of text.

like image 78
marto Avatar answered Oct 27 '22 04:10

marto