zoukankan      html  css  js  c++  java
  • python正则表达式匹配中文

    # -*- coding: utf-8 -*-
    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.encode("utf8")
            print
     
    text ="#who#helloworld#a中文x#"
    usample=unicode(text,'utf8')
    findPart(u"#[\w\u2E80-\u9FFF]+#", usample, "unicode chinese")
    注:
    几个主要非英文语系字符范围
    2E80~33FFh:中日韩符号区。收容康熙字典部首、中日韩辅助部首、注音符号、日本假名、韩文音符,中日韩的符号、标点、带圈或带括符文数字、月份,以及日本的假名组合、单位、年号、月份、日期、时间等。
    3400~4DFFh:中日韩认同表意文字扩充A区,总计收容6,582个中日韩汉字。
    4E00~9FFFh:中日韩认同表意文字区,总计收容20,902个中日韩汉字。
    A000~A4FFh:彝族文字区,收容中国南方彝族文字和字根。
    AC00~D7FFh:韩文拼音组合字区,收容以韩文音符拼成的文字。
    F900~FAFFh:中日韩兼容表意文字区,总计收容302个中日韩汉字。
    FB00~FFFDh:文字表现形式区,收容组合拉丁文字、希伯来文、阿拉伯文、中日韩直式标点、小符号、半角符号、全角
    
    
    
    
    (
    #!/usr/bin/python3
    # -*- coding: UTF-8 -*-
     
    import re
    message = u'天人合一'.encode('utf8')
    print(re.search(u'人'.encode('utf8'), message).group())
    交互模式下的例子

          >>> import re
          >>> s='Phone No. 010-87654321'
          >>>
          >>> r=re.compile(r'(\d+)-(\d+)')
          >>> m=r.search(s)
          >>> m
           <_sre.SRE_Match object at 0x010EE218>

    )
  • 相关阅读:
    python开发环境安装
    python文件I/O
    python字符串方法以及注释
    python列表
    php: Can't use function return value in write context
    Notice : brew install php70
    对web开发从业者的发展方向的思考
    关于微信跨号支付
    MySQL触发器写法
    MySQL慢查询日志
  • 原文地址:https://www.cnblogs.com/djcsch2001/p/2029270.html
Copyright © 2011-2022 走看看