python中正則表達(dá)式匹配中文,首先需要確保所有編碼都為 unicode(python3已經(jīng)默認(rèn)都是unicode編碼,所以就沒有這個(gè)困擾,需要特別注意的是python2版本這部分的正則表達(dá)式)
漢字的范圍為”\u4e00-\u9fa5“
>>> test="hello,張sir,最近過得怎么樣?"
>>> re.findall('[\u4e00-\u9fa5]',test)#匹配中文
['張', '最', '近', '過', '得', '怎', '么', '樣']
>>> re.findall('[^\u4e00-\u9fa5]',test)#匹配非中文
['h', 'e', 'l', 'l', 'o', ',', 's', 'i', 'r', ',', '?']