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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With