C # How to replace a string without an accent with a regex?

I want to do an accent replacement in a string. I want the "client" to match "cliënt" and vice versa.

My code is as follows:

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

So, how can I make sure that “client” matches “client” and “cliënt” and vice versa?

+6
source share
2 answers

You can include it in 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 effective, but will do the job with a bit of text.

+3
source

Look at this page

Keep in mind that you need to work in a certain culture, although there is no arbitrary culture of replacing emphasis, since in one culture e and ë can be considered equivalent, in another they can be different.

0
source

Source: https://habr.com/ru/post/888223/


All Articles