代码改变世界

python 正则表达式匹配中文(转)

2013-07-27 23:40  江湖么名  阅读(3733)  评论(0编辑  收藏  举报

网上的一篇文章,做了整理,作者已无从考证,谢谢了

  1 s="""
  2 en: Regular expression is a powerful tool for manipulating text. 
  3 zh: 中文 
  4 jp: 正規表現は非常に役に立つツールテキストを操作することです。 
  5 jp-char: あアいイうウえエおオ 
  6 kr:정규 표현식은 매우 유용한 도구 텍스트를 조작하는 것입니다. 
  7 puc: 。?!、,;:“ ”‘ '——……·-·《》〈〉!¥%&*# 
  8 """
  9 print "原始utf8字符"
 10 #utf8
 11 print "--------"
 12 print repr(s)
 13 print "--------\n"
 14 
 15 #非ansi
 16 re_words=re.compile(r"[\x80-\xff]+")
 17 m =  re_words.search(s,0)
 18 print "非ansi字符"
 19 print "--------"
 20 print m
 21 print m.group()
 22 print "--------\n"
 23 
 24 #unicode
 25 s = unicode(s)
 26 print "原始unicode字符"
 27 print "--------"
 28 print repr(s)
 29 print "--------\n"
 30 
 31 #unicode chinese
 32 re_words = re.compile(u"[\u4e00-\u9fa5]+")
 33 m =  re_words.search(s,0)
 34 print "unicode 中文"
 35 print "--------"
 36 print m
 37 print m.group()
 38 print "--------\n"
 39 
 40 
 41 #unicode korean
 42 re_words=re.compile(u"[\uac00-\ud7ff]+")
 43 m =  re_words.search(s,0)
 44 print "unicode 韩文"
 45 print "--------"
 46 print m
 47 print m.group()
 48 print "--------\n"
 49 
 50 
 51 #unicode japanese katakana
 52 re_words=re.compile(u"[\u30a0-\u30ff]+")
 53 m =  re_words.search(s,0)
 54 print "unicode 日文 片假名"
 55 print "--------"
 56 print m
 57 print m.group()
 58 print "--------\n"
 59 
 60 
 61 #unicode japanese hiragana
 62 re_words=re.compile(u"[\u3040-\u309f]+")
 63 m =  re_words.search(s,0)
 64 print "unicode 日文 平假名"
 65 print "--------"
 66 print m
 67 print m.group()
 68 print "--------\n"
 69 
 70 
 71 #unicode cjk Punctuation
 72 re_words=re.compile(u"[\u3000-\u303f\ufb00-\ufffd]+")
 73 m =  re_words.search(s,0)
 74 print "unicode 标点符号"
 75 print "--------"
 76 print m
 77 print m.group()
 78 print "--------\n"
 79 
 80 
 81 -------------------------------------------------------
 82 原始utf8字符
 83 --------
 84 "\nen: Regular expression is a powerful tool for manipulating text. \nzh: \xe4\xb8\xad\xe6\x96\x87 \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\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"
 85 --------
 86 
 87 非ansi字符
 88 --------
 89 <_sre.SRE_Match object at 0x01A6C330>
 90 中文
 91 --------
 92 
 93 原始unicode字符
 94 --------
 95 u"\nen: Regular expression is a powerful tool for manipulating text. \nzh: \u4e2d\u6587 \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 '\u2014\u2014\u2026\u2026\xb7\uff0d\xb7\u300a\u300b\u3008\u3009\uff01\uffe5\uff05\uff06\uff0a\uff03 \n"
 96 --------
 97 
 98 unicode 中文
 99 --------
100 <_sre.SRE_Match object at 0x014F68A8>
101 中文
102 --------
103 
104 unicode 韩文
105 --------
106 <_sre.SRE_Match object at 0x01A6C330>
107 정규
108 --------
109 
110 unicode 日文 片假名
111 --------
112 <_sre.SRE_Match object at 0x014F68A8>
113 ツールテキスト
114 --------
115 
116 unicode 日文 平假名
117 --------
118 <_sre.SRE_Match object at 0x01A6C330>
119 120 --------
121 
122 unicode 标点符号
123 --------
124 <_sre.SRE_Match object at 0x014F68A8>
125 126 --------