1. 程式人生 > >python正則表示式筆記

python正則表示式筆記

從字串的角度來說,中文不如英文整齊、規範,這是不可避免的現實。本文結合網上資料以及個人經驗,以 python 語言為例,稍作總結。歡迎補充或挑錯。 
一點經驗 
可以使用 repr()函式檢視字串的原始格式。這對於寫正則表示式有所幫助。 
Python 的 re模組有兩個相似的函式:re.match(), re.search 。兩個函式的匹配過程完全一致,只是起點不同。match只從字串的開始位置進行匹配,如果失敗,它就此放棄;而search則會鍥而不捨地完全遍歷整個字串中所有可能的位置,直到成功地找到一個匹配,或者搜尋完字串,以失敗告終。如果你瞭解match的特性(在某些情況下比較快),大可以自由用它;如果不太清楚,search通常是你需要的那個函式。 

從一堆文字中,找出所有可能的匹配,以列表的形式返回,這種情況用findall()這個函式。例子見後面的程式碼。 
utf8下,每個漢字佔據3個字元位置,正則式為[\x80-\xff]{3},這個都知道了吧。 
unicode下,漢字的格式如\uXXXX,只要找到對應的字符集的範圍,就能匹配相應的字串,方便從多語言文字中挑出所需要的某種語言的文字。不過,對於像日文這樣的粘著語,既有中文字元,又有平假名片假名,或許結果會有所偏差。 
兩種字元類可以並列在一起使用,例如,平假名、片假名、中文的放在一起,u"[\u4e00-\u9fa5\u3040-\u309f\u30a0-\u30ff]+",來自定義所需要匹配的文字。 
匹配中文時,正則表示式和目標字串的格式必須相同。這一點至關重要。或者都用預設的utf8,此時你不用額外做什麼;如果是unicode,就需要在正則式之前加上u""格式。 

可以這樣定義unicode字串:string=u"我愛正則表示式"。如果字串不是unicode的,可以使用unicode()函式轉換之。如果你知道源字串的編碼,可以使用newstr=unicode(oldstring, original_coding_name)的方式轉換,例如 linux 下常用unicode(string, "utf8"),windows 下或許會用cp936吧,沒測試。 

例程式 

 
#!/usr/bin/python 
# -*- 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 
print 
#sample is utf8 by default. 
sample='''en: Regular expression is a powerful tool for manipulating text. 
zh: 正則表示式是一種很有用的處理文字的工具。 
jp: 正規表現は非常に役に立つツールテキストを操作することです。 
jp-char: あアいイうウえエおオ 
kr:정규 표현식은 매우 유용한 도구 텍스트를 조작하는 것입니다. 
puc: 。?!、,;:“ ”‘ '——……·-·《》〈〉!¥%&*# 
''' 
#let's look its raw representation under the hood: 
print "the raw utf8 string is:\n", repr(sample) 
print 
#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) 
print 
#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-\u303f\ufb00-\ufffd]+", usample, "unicode cjk Punctuation") 

其輸出結果為:
 
the raw utf8 string is: 
'en: Regular expression is a powerful tool for manipulating text.\nzh: \xe6\xad\xa3\xe5\x88\x99\xe8\xa1\xa8\xe8\xbe\xbe\xe5\xbc\x8f\xe6\x98\xaf\xe4\xb8\x80\xe7\xa7\x8d\xe5\xbe\x88\xe6\x9c\x89\xe7\x94\xa8\xe7\x9a\x84\xe5\xa4\x84\xe7\x90\x86\xe6\x96\x87\xe6\x9c\xac\xe7\x9a\x84\xe5\xb7\xa5\xe5\x85\xb7\xe3\x80\x82\njp: \xe6\xad\xa3\xe8\xa6\x8f\xe8\xa1\xa8\xe7\x8f\xbe\xe3\x81\xaf\xe9\x9d\x9e\xe5\xb8\xb8\xe3\x81\xab\xe5\xbd\xb9\xe3\x81\xab\xe7\xab\x8b\xe3\x81\xa4\xe3\x83\x84\xe3\x83\xbc\xe3\x83\xab\xe3\x83\x86\xe3\x82\xad\xe3\x82\xb9\xe3\x83\x88\xe3\x82\x92\xe6\x93\x8d\xe4\xbd\x9c\xe3\x81\x99\xe3\x82\x8b\xe3\x81\x93\xe3\x81\xa8\xe3\x81\xa7\xe3\x81\x99\xe3\x80\x82\njp-char: \xe3\x81\x82\xe3\x82\xa2\xe3\x81\x84\xe3\x82\xa4\xe3\x81\x86\xe3\x82\xa6\xe3\x81\x88\xe3\x82\xa8\xe3\x81\x8a\xe3\x82\xaa\nkr:\xec\xa0\x95\xea\xb7\x9c \xed\x91\x9c\xed\x98\x84\xec\x8b\x9d\xec\x9d\x80 \xeb\xa7\xa4\xec\x9a\xb0 \xec\x9c\xa0\xec\x9a\xa9\xed\x95\x9c \xeb\x8f\x84\xea\xb5\xac \xed\x85\x8d\xec\x8a\xa4\xed\x8a\xb8\xeb\xa5\xbc \xec\xa1\xb0\xec\x9e\x91\xed\x95\x98\xeb\x8a\x94 \xea\xb2\x83\xec\x9e\x85\xeb\x8b\x88\xeb\x8b\xa4.\npuc: \xe3\x80\x82\xef\xbc\x9f\xef\xbc\x81\xe3\x80\x81\xef\xbc\x8c\xef\xbc\x9b\xef\xbc\x9a\xe2\x80\x9c \xe2\x80\x9d\xe2\x80\x98 \xe2\x80\x99\xe2\x80\x94\xe2\x80\x94\xe2\x80\xa6\xe2\x80\xa6\xc2\xb7\xef\xbc\x8d\xc2\xb7\xe3\x80\x8a\xe3\x80\x8b\xe3\x80\x88\xe3\x80\x89\xef\xbc\x81\xef\xbf\xa5\xef\xbc\x85\xef\xbc\x86\xef\xbc\x8a\xef\xbc\x83\n' 
There are 14 non-ascii parts: 
正則表示式是一種很有用的處理文字的工具。 
正規表現は非常に役に立つツールテキストを操作することです。 
あアいイうウえエおオ 
정규 
표현식은 
매우 
유용한 
도구 
텍스트를 
조작하는 
것입니다 
。?!、,;:“ 
”‘ 
'——……·-·《》〈〉!¥%&*# 
the raw unicode string is: 
u'en: Regular expression is a powerful tool for manipulating text.\nzh: \u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f88\u6709\u7528\u7684\u5904\u7406\u6587\u672c\u7684\u5de5\u5177\u3002\njp: \u6b63\u898f\u8868\u73fe\u306f\u975e\u5e38\u306b\u5f79\u306b\u7acb\u3064\u30c4\u30fc\u30eb\u30c6\u30ad\u30b9\u30c8\u3092\u64cd\u4f5c\u3059\u308b\u3053\u3068\u3067\u3059\u3002\njp-char: \u3042\u30a2\u3044\u30a4\u3046\u30a6\u3048\u30a8\u304a\u30aa\nkr:\uc815\uaddc \ud45c\ud604\uc2dd\uc740 \ub9e4\uc6b0 \uc720\uc6a9\ud55c \ub3c4\uad6c \ud14d\uc2a4\ud2b8\ub97c \uc870\uc791\ud558\ub294 \uac83\uc785\ub2c8\ub2e4.\npuc: \u3002\uff1f\uff01\u3001\uff0c\uff1b\uff1a\u201c \u201d\u2018 \u2019\u2014\u2014\u2026\u2026\xb7\uff0d\xb7\u300a\u300b\u3008\u3009\uff01\uffe5\uff05\uff06\uff0a\uff03\n' 
There are 6 unicode chinese parts: 
正則表示式是一種很有用的處理文字的工具 
正規表現 
非常 
役 
立 
操作 
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: 
。 
。 
。?!、,;: 
- 
《》〈〉!¥%&*#