利用正則表達式分離漢字、英文、數字


在中文分詞的過程中需要將英文,數字,漢字分離,數字和英文就不用分割了,主要是將分離出來的漢字進行分詞,下面的算法實現利用正則表達式分離漢字、英文、數字:

//獲取中文
string chRegS = @"[\u4e00-\u9fa5]+";
Regex chRegR = new Regex(chRegS);
Match chMacth = chRegR.Match(str);
while(chMacth.Success)
{
     CHresult.Add(chMacth.ToString());
     chMacth = chMacth.NextMatch();
}
//英文
string enRegS = @"[a-zA-Z]+";
Regex enRegR = new Regex(enRegS);
Match enMatch = enRegR.Match(str);
while (enMatch.Success)
{
     Enresult.Add(enMatch.ToString());
     enMatch = enMatch.NextMatch();
}
//數字
string numRegS = @"\d+";
Regex numRegR = new Regex(numRegS);
Match numMatch = numRegR.Match(str);
while (numMatch.Success)
{
     Numresult.Add(numMatch.ToString());
     numMatch = numMatch.NextMatch();
}

測試字符串:“可復用的WPF或者Silverlight應用程序和組件設計(3)——控件級別”

結果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM