Python 中文正则笔记

Friday, July 9, 2010
总结在 python 语言里使用正则表达式匹配中文的经验。关键词:中文,cjk,utf8,unicode,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吧,没测试。
例程序
# -*- 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:
u6b63u5219u8868u8fbeu5f0fu662fu4e00u79cdu5f88u6709u7528u7684u5904u7406u6587u672cu7684u5de5u5177u3002njp:
u6b63u898fu8868u73feu306fu975eu5e38u306bu5f79u306bu7acbu3064u30c4u30fcu30ebu30c6u30adu30b9u30c8u3092u64cdu4f5cu3059u308bu3053u3068u3067u3059u3002njp-char:
u3042u30a2u3044u30a4u3046u30a6u3048u30a8u304au30aankr:uc815uaddc
ud45cud604uc2dduc740 ub9e4uc6b0 uc720uc6a9ud55c ub3c4uad6c
ud14duc2a4ud2b8ub97c uc870uc791ud558ub294 uac83uc785ub2c8ub2e4.npuc:
u3002uff1fuff01u3001uff0cuff1buff1au201c u201du2018
u2019u2014u2014u2026u2026xb7uff0dxb7u300au300bu3008u3009uff01uffe5uff05uff06uff0auff03n'


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:




。?!、,;:

《》〈〉!¥%&*#</pre>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值