Regular expressions (regex) in Japanese

前端 未结 4 1101
你的背包
你的背包 2020-12-29 08:28

I am learning about Regular expressions (regex) for English and although some of the concepts seem like they would apply to other languages such as Japanese, I feel as if ma

4条回答
  •  伪装坚强ぢ
    2020-12-29 09:21

    For Python

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    import re
    
    kanji = u'漢字'
    hiragana = u'ひらがな'
    katakana = u'カタカナ'
    str = kanji + hiragana + katakana
    
    #Match Kanji
    regex = u'[\u4E00-\u9FFF]+' # == u'[一-龠々]+'
    match = re.search(regex, str, re.U)
    print match.group().encode('utf-8') #=> 漢字
    
    #Match Hiragana
    regex = u'[\u3040-\u309Fー]+' # == u'[ぁ-んー]+'
    match = re.search(regex, str, re.U)
    print match.group().encode('utf-8') #=> ひらがな
    
    #Match Katakana
    regex = u'[\u30A0-\u30FF]+' # == u'[ァ-ヾ]+'
    match = re.search(regex, str, re.U)
    print match.group().encode('utf-8') #=>カタカナ
    

提交回复
热议问题