这是我的业余爱好
RemoveDiacritics
fiddle
):
public static string RemoveDiacritics(Dictionary<char, char[]> exclude, string source)
{
// Exclude letters (using a lookbehind), include diacritics
string match = "(?<=[×-ת])[\u05b0-\u05c2]";
// Prepare the exclusion group
string exclusionGroup = string.Join("|", exclude.Select(p =>
string.Concat(p.Key, string.Join(string.Empty, p.Value)))
);
// Create the exclusion group (using a lookahead)
string except = $"(?!{exclusionGroup})";
// Do the match
return Regex.Replace(source, string.Concat(except, match), string.Empty);
}
我测试了它:
static void Main(string[] args)
{
string source = "×Ö¸×Ö´××ªÖ´× ×Ö¼Ö°×Ö´×Ö¼Ö·×¢Ö·× ×ֶעָש×Ö¸× ×Ö¼Ö°×§Ö´×Ö¼×Ö¼×¨Öµ× ×ָרֶסֶס ×Ö·×Ö¼Ö¸×Ö¸×";
Dictionary<char, char[]> exclude = new Dictionary<char, char[]>
{
{'\u05db', new char[] {'\u05bc' } }, // ×Ö¼
{'\u05d1', new char[] {'\u05bc', '\u05b7' } }, // ×Ö¼Ö·
};
string replaced = RemoveDiacritics(exclude, source);
}
"××××ª× ×Ö¼××Ö·Ö¼×¢× ××¢×©× ××§×××¨× ×רסס ××××"
(第二个单词上只有两个字母应该有变音符号)。
"××××ª× ×Ö°×Ö´×Ö·×¢× ××¢×©Ö¸× ×Ö°×§×××¨× ×רסס ××Ö¸××"
在我的实际结果中,你可以看到:
-
任何一封信
'\u05bc'
里面
一个字母)和一个附加的变音符号一起,被错误地用那个附加的变音符号留下。
-
×Ö´
和
שָ
(他们在2号&分别是第三个单词)。不知道为什么。
我该怎么做?