简体   繁体   English

regex.replace C#变音符号

[英]regex.replace c# diacritics

I'm trying to replace strings with diacritics, but no luck. 我正在尝试用变音符号替换字符串,但是没有运气。 I need to replace, for example "Ю" and "ю" to "yu", "Б" and "б" to "b" and so on with my own table. 我需要用自己的表替换例如“Ю”和“ю”替换为“ yu”,“Б”和“б”替换为“ b”等。 I have a such code, which is not working: 我有这样的代码,这是行不通的:

case "LastRUEN":
                if (csentry["LAST"].IsPresent)
                {
                    string FIELD_RU = csentry["LAST"].Value;
                    string FIELD_EN;
                    FIELD_EN = Regex.Replace(FIELD_RU, "[Аа]", "a");
                    FIELD_EN = Regex.Replace(FIELD_RU, "[Бб]", "b");
                    FIELD_EN = Regex.Replace(FIELD_RU, "[Вв]", "v");
                    FIELD_EN = Regex.Replace(FIELD_RU, "[Гг]", "h");
                    FIELD_EN = Regex.Replace(FIELD_RU, "[Ґґ]", "g");
                    FIELD_EN = Regex.Replace(FIELD_RU, "[Дд]", "d");
                    FIELD_EN = Regex.Replace(FIELD_RU, "[Ее]", "e");
                    mventry["lastNameEN"].Value = FIELD_EN;
}
break;

Can anybody hepl with troubleshooting? 有人可以帮助进行故障排除吗? Maybe it would be better to use a method for my case? 也许对我的情况使用一种方法会更好? Thanks! 谢谢!

This should work better, because the result of the Replace calls will be the input of the next operation. 这应该更好地工作,因为Replace调用的结果将是下一个操作的输入。

case "LastRUEN":
            if (csentry["LAST"].IsPresent)
            {
                string FIELD_RU = csentry["LAST"].Value;
                string FIELD_EN;
                FIELD_EN = Regex.Replace(FIELD_RU, "[Аа]", "a");
                FIELD_EN = Regex.Replace(FIELD_EN , "[Бб]", "b");
                FIELD_EN = Regex.Replace(FIELD_EN , "[Вв]", "v");
                FIELD_EN = Regex.Replace(FIELD_EN , "[Гг]", "h");
                FIELD_EN = Regex.Replace(FIELD_EN , "[Ґґ]", "g");
                FIELD_EN = Regex.Replace(FIELD_EN , "[Дд]", "d");
                FIELD_EN = Regex.Replace(FIELD_EN , "[Ее]", "e");
                mventry["lastNameEN"].Value = FIELD_EN;
}
break;

And of course you should move the operations into a separate method. 当然,您应该将操作移到单独的方法中。

EDIT: Replace Multiple Characters in a String is interessing to combine the multiple calls. 编辑: 替换字符串中的多个字符正在插入以合并多个调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM