如何在C#中使用正则表达式来匹配中文字符而不匹配日语假名?

时间:2018-03-10 03:03:39

标签: c# .net uwp

在C#中,\ p {Han}匹配中文字符和日文平假名和片假名。我想区分它们,所以我该怎么办?将每个char转换为unicode,然后检测该字符是否在范围内?

1 个答案:

答案 0 :(得分:1)

//For chinese chars
public bool IsChinese(string text)
{
    return text.Any(c => c >= 0x20000 && c <= 0xFA2D);
}

//For japanese chars
private static IEnumerable<char> GetCharsInRange(string text, int min, int max)
{
    return text.Where(e => e >= min && e <= max);
}

用法:

var romaji = GetCharsInRange(searchKeyword, 0x0020, 0x007E);
var hiragana = GetCharsInRange(searchKeyword, 0x3040, 0x309F);
var katakana = GetCharsInRange(searchKeyword, 0x30A0, 0x30FF);
var kanji = GetCharsInRange(searchKeyword, 0x4E00, 0x9FBF);