C# How to replace an accent insensitive string with regex?
I'd like to perform an accent-insensitive replace in a string. I want 'client' t开发者_如何学Goo 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.
Have a look at this page
Bear in mind that you need to work in a specific culture though - there's no arbitrary accent-replacement culture, since in one culture e and ë might be considered equivalent, in another they might be different.
精彩评论