從字符串的角度來說,中文不如英文整齊、規(guī)范,這是不可避免的現(xiàn)實。本文結合網(wǎng)上資料以及個人經(jīng)驗,以 python 語言為例,稍作總結。歡迎補充或挑錯。
一點經(jīng)驗
可以使用 repr()函數(shù)查看字串的原始格式。這對于寫正則表達式有所幫助。
Python 的 re模塊有兩個相似的函數(shù):re.match(), re.search 。兩個函數(shù)的匹配過程完全一致,只是起點不同。match只從字串的開始位置進行匹配,如果失敗,它就此放棄;而search則會鍥而不舍地完全遍歷整個字串中所有可能的位置,直到成功地找到一個匹配,或者搜索完字串,以失敗告終。如果你了解match的特性(在某些情況下比較快),大可以自由用它;如果不太清楚,search通常是你需要的那個函數(shù)。
從一堆文本中,找出所有可能的匹配,以列表的形式返回,這種情況用findall()這個函數(shù)。例子見后面的代碼。
utf8下,每個漢字占據(jù)3個字符位置,正則式為[x80-xff]{3},這個都知道了吧。
unicode下,漢字的格式如uXXXX,只要找到對應的字符集的范圍,就能匹配相應的字串,方便從多語言文本中挑出所需要的某種語言的文本。不過,對于像日文這樣的粘著語,既有中文字符,又有平假名片假名,或許結果會有所偏差。
兩種字符類可以并列在一起使用,例如,平假名、片假名、中文的放在一起,u”[u4e00-u9fa5u3040-u309fu30a0-u30ff]+”,來自定義所需要匹配的文本。
匹配中文時,正則表達式和目標字串的格式必須相同。這一點至關重要?;蛘叨加媚J的utf8,此時你不用額外做什么;如果是unicode,就需要在正則式之前加上u””格式。
可以這樣定義unicode字符串:string=u”我愛正則表達式”。如果字串不是unicode的,可以使用unicode()函數(shù)轉換之。如果你知道源字串的編碼,可以使用newstr=unicode(oldstring, original_coding_name)的方式轉換,例如 linux 下常用unicode(string, “utf8”),windows 下或許會用cp936吧,沒測試。
例程序
# -*- coding: utf-8 -*-
#
#author: rex
#blog: http://iregex.org
#filename py_utf8_unicode.py
#created: 2010-06-27 09:11
import re
def findPart(regex, text, name):
res=re.findall(regex, text)
if res:
print “There are %d %s parts:n”% (len(res), name)
for r in res:
print “t”,r
#sample is utf8 by default.
sample=”’en: Regular expression is a powerful tool for manipulating text.
zh: 正則表達式是一種很有用的處理文本的工具。
jp: 正表Fは非常に役に立つツ`ルテキストを操作することです。
jp-char: あアいイうウえエおオ
kr:정규 표현식은 매우 유용한 도구 텍스트를 조작하는 것입니다.
puc: 。?!、,;:“ ”‘ ‘――……?-?《》〈〉?。ぃィΓ?
”’
#let’s look its raw representation under the hood:
print “the raw utf8 string is:n”, repr(sample)
#find the non-ascii chars:
findPart(r”[x80-xff]+”,sample,”non-ascii”)
#convert the utf8 to unicode
usample=unicode(sample,’utf8′)
#let’s look its raw representation under the hood:
print “the raw unicode string is:n”, repr(usample)
#get each language parts:
findPart(u”[u4e00-u9fa5]+”, usample, “unicode chinese”)
findPart(u”[uac00-ud7ff]+”, usample, “unicode korean”)
findPart(u”[u30a0-u30ff]+”, usample, “unicode japanese katakana”)
findPart(u”[u3040-u309f]+”, usample, “unicode japanese hiragana”)
findPart(u”[u3000-u303fufb00-ufffd]+”, usample, “unicode cjk Punctuation”)
其輸出結果為:
‘en: Regular expression is a powerful tool for manipulating text.nzh: xe6xadxa3xe5x88x99xe8xa1xa8xe8xbexbexe5xbcx8fxe6x98xafxe4xb8x80xe7xa7x8dxe5xbex88xe6x9cx89xe7x94xa8xe7x9ax84xe5xa4x84xe7x90x86xe6x96x87xe6x9cxacxe7x9ax84xe5xb7xa5xe5x85xb7xe3x80x82njp: xe6xadxa3xe8xa6x8fxe8xa1xa8xe7x8fxbexe3x81xafxe9x9dx9exe5xb8xb8xe3x81xabxe5xbdxb9xe3x81xabxe7xabx8bxe3x81xa4xe3x83x84xe3x83xbcxe3x83xabxe3x83x86xe3x82xadxe3x82xb9xe3x83x88xe3x82x92xe6x93x8dxe4xbdx9cxe3x81x99xe3x82x8bxe3x81x93xe3x81xa8xe3x81xa7xe3x81x99xe3x80x82njp-char: xe3x81x82xe3x82xa2xe3x81x84xe3x82xa4xe3x81x86xe3x82xa6xe3x81x88xe3x82xa8xe3x81x8axe3x82xaankr:xecxa0x95xeaxb7x9c xedx91x9cxedx98x84xecx8bx9dxecx9dx80 xebxa7xa4xecx9axb0 xecx9cxa0xecx9axa9xedx95x9c xebx8fx84xeaxb5xac xedx85x8dxecx8axa4xedx8axb8xebxa5xbc xecxa1xb0xecx9ex91xedx95x98xebx8ax94 xeaxb2x83xecx9ex85xebx8bx88xebx8bxa4.npuc: xe3x80x82xefxbcx9fxefxbcx81xe3x80x81xefxbcx8cxefxbcx9bxefxbcx9axe2x80x9c xe2x80x9dxe2x80x98 xe2x80x99xe2x80x94xe2x80x94xe2x80xa6xe2x80xa6xc2xb7xefxbcx8dxc2xb7xe3x80x8axe3x80x8bxe3x80x88xe3x80x89xefxbcx81xefxbfxa5xefxbcx85xefxbcx86xefxbcx8axefxbcx83n’
There are 14 non-ascii parts:
正則表達式是一種很有用的處理文本的工具。
正表Fは非常に役に立つツ`ルテキストを操作することです。
あアいイうウえエおオ
정규
표현식은
매우
유용한
도구
텍스트를
조작하는
것입니다
。?!、,;:“
”‘
‘――……?-?《》〈〉?。ぃィΓ?
the raw unicode string is:
u’en: Regular expression is a powerful tool for manipulating text.nzh: u6b63u5219u8868u8fbeu5f0fu662fu4e00u79cdu5f88u6709u7528u7684u5904u7406u6587u672cu7684u5de5u5177u3002njp: u6b63u898fu8868u73feu306fu975eu5e38u306bu5f79u306bu7acbu3064u30c4u30fcu30ebu30c6u30adu30b9u30c8u3092u64cdu4f5cu3059u308bu3053u3068u3067u3059u3002njp-char: u3042u30a2u3044u30a4u3046u30a6u3048u30a8u304au30aankr:uc815uaddc ud45cud604uc2dduc740 ub9e4uc6b0 uc720uc6a9ud55c ub3c4uad6c ud14duc2a4ud2b8ub97c uc870uc791ud558ub294 uac83uc785ub2c8ub2e4.npuc: u3002uff1fuff01u3001uff0cuff1buff1au201c u201du2018 u2019u2014u2014u2026u2026xb7uff0dxb7u300au300bu3008u3009uff01uffe5uff05uff06uff0auff03n’
There are 6 unicode chinese parts:
正則表達式是一種很有用的處理文本的工具
正表F
非常
役
立
操作
There are 8 unicode korean parts:
정규
표현식은
매우
유용한
도구
텍스트를
조작하는
것입니다
There are 6 unicode japanese katakana parts:
ツ`ルテキスト
ア
イ
ウ
エ
オ
There are 11 unicode japanese hiragana parts:
は
に
に
つ
を
することです
あ
い
う
え
お
There are 5 unicode cjk Punctuation parts:
。
。
。?!、,;:
-
《》〈〉?。ぃィΓ?