在中文分詞的過程中需要將英文,數字,漢字分離,數字和英文就不用分割了,主要是將分離出來的漢字進行分詞,下面的算法實現利用正則表達式分離漢字、英文、數字:
//獲取中文 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)——控件級別”
結果:

