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

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

      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 "--------
    "
     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 "--------
    "
     23 
     24 #unicode
     25 s = unicode(s)
     26 print "原始unicode字符"
     27 print "--------"
     28 print repr(s)
     29 print "--------
    "
     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 "--------
    "
     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 "--------
    "
     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 "--------
    "
     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 "--------
    "
     69 
     70 
     71 #unicode cjk Punctuation
     72 re_words=re.compile(u"[u3000-u303fufb00-ufffd]+")
     73 m =  re_words.search(s,0)
     74 print "unicode 标点符号"
     75 print "--------"
     76 print m
     77 print m.group()
     78 print "--------
    "
     79 
     80 
     81 -------------------------------------------------------
     82 原始utf8字符
     83 --------
     84 "
    en: Regular expression is a powerful tool for manipulating text. 
    zh: xe4xb8xadxe6x96x87 
    jp: xe6xadxa3xe8xa6x8fxe8xa1xa8xe7x8fxbexe3x81xafxe9x9dx9exe5xb8xb8xe3x81xabxe5xbdxb9xe3x81xabxe7xabx8bxe3x81xa4xe3x83x84xe3x83xbcxe3x83xabxe3x83x86xe3x82xadxe3x82xb9xe3x83x88xe3x82x92xe6x93x8dxe4xbdx9cxe3x81x99xe3x82x8bxe3x81x93xe3x81xa8xe3x81xa7xe3x81x99xe3x80x82 
    jp-char: xe3x81x82xe3x82xa2xe3x81x84xe3x82xa4xe3x81x86xe3x82xa6xe3x81x88xe3x82xa8xe3x81x8axe3x82xaa 
    kr:xecxa0x95xeaxb7x9c xedx91x9cxedx98x84xecx8bx9dxecx9dx80 xebxa7xa4xecx9axb0 xecx9cxa0xecx9axa9xedx95x9c xebx8fx84xeaxb5xac xedx85x8dxecx8axa4xedx8axb8xebxa5xbc xecxa1xb0xecx9ex91xedx95x98xebx8ax94 xeaxb2x83xecx9ex85xebx8bx88xebx8bxa4. 
    puc: xe3x80x82xefxbcx9fxefxbcx81xe3x80x81xefxbcx8cxefxbcx9bxefxbcx9axe2x80x9c xe2x80x9dxe2x80x98 'xe2x80x94xe2x80x94xe2x80xa6xe2x80xa6xc2xb7xefxbcx8dxc2xb7xe3x80x8axe3x80x8bxe3x80x88xe3x80x89xefxbcx81xefxbfxa5xefxbcx85xefxbcx86xefxbcx8axefxbcx83 
    "
     85 --------
     86 
     87 非ansi字符
     88 --------
     89 <_sre.SRE_Match object at 0x01A6C330>
     90 中文
     91 --------
     92 
     93 原始unicode字符
     94 --------
     95 u"
    en: Regular expression is a powerful tool for manipulating text. 
    zh: u4e2du6587 
    jp: u6b63u898fu8868u73feu306fu975eu5e38u306bu5f79u306bu7acbu3064u30c4u30fcu30ebu30c6u30adu30b9u30c8u3092u64cdu4f5cu3059u308bu3053u3068u3067u3059u3002 
    jp-char: u3042u30a2u3044u30a4u3046u30a6u3048u30a8u304au30aa 
    kr:uc815uaddc ud45cud604uc2dduc740 ub9e4uc6b0 uc720uc6a9ud55c ub3c4uad6c ud14duc2a4ud2b8ub97c uc870uc791ud558ub294 uac83uc785ub2c8ub2e4. 
    puc: u3002uff1fuff01u3001uff0cuff1buff1au201c u201du2018 'u2014u2014u2026u2026xb7uff0dxb7u300au300bu3008u3009uff01uffe5uff05uff06uff0auff03 
    "
     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 --------
  • 相关阅读:
    Reactive Extensions (Rx) 入门(5) —— Rx的事件编程
    Reactive Extensions (Rx) 入门(4) —— Rx的事件编程
    Reactive Extensions (Rx) 入门(3) —— Rx的事件编程
    Reactive Extensions (Rx) 入门(2) —— 安装 Reactive Extensions
    Reactive Extensions (Rx) 入门(1) —— Reactive Extensions 概要
    Xamarin NuGet 缓存包导致 already added : Landroid/support/annotation/AnimRes 问题解决方案
    Android 系统Action大全
    Xamarin Forms 实现发送通知点击跳转
    如何理解灰度发布
    推荐一款分布式微服务框架 Surging
  • 原文地址:https://www.cnblogs.com/mmix2009/p/3220476.html
Copyright © 2011-2022 走看看