![](/img/trans.png)
[英]How to make an accent insensitive search in elasticsearch with nest c# client?
[英]C# How to replace an accent insensitive string with regex?
我想在字符串中執行不區分重音的替換。 我希望'客戶'匹配'cliënt',反之亦然。
我的代碼看起來像這樣:
Regex reg = new Regex("client");
string result = reg.Replace("here goes the content with client and cliënt", "replacementWith");
那么,我如何確保'客戶'匹配'客戶'和'cliënt',反之亦然?
您可以將其包含在Regex中
Regex reg = new Regex("cli[eë]nt"); // will match both 'client' and 'cliënt'
或者您可以刪除字符串中的所有重音符,然后應用正則表達式。
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]);
}
這不是很有效,但會為少量文本做好准備。
請記住,你需要在特定的文化中工作 - 沒有任意的口音替代文化,因為在一種文化中,e和ë可能被認為是等同的,在另一種文化中,它們可能是不同的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.