-1

How can I detect chinese, japanese and korean characters with regex in python. I've tried many things including many recommendations on Stack Overflow, but nothing has yet worked.

Example word string:

word_string = "direct tv カルバンクライン 評価 カルバンクライン 価格 赤ジャージ アディダス nike エアリフト dg コート dg ネクタイ fresh 香水 フランス fresh 香水 ハワイ 1directtvcom ビームス バンズ コラボ httpwww1directtvcom 厚底コンバース 通販 フルラ バッグ 値段 プーマ専門店 ドクターマーチン ショート フルラ バッグ 新作 2015 スタッズグラディエーターサンダル モード・エ・ジャコモ mode et jacomo parker パーカー 万年筆 デュオフォールド" 

Example 1:

>> print re.findall(r'[\p{IsHiragana}\p{IsKatakana}\p{IsHan}]+', word_string) ['ir', 't', 't', 'nik', 'g', 'g', 'r', 's', 'r', 's', 'ir', 'tt', 'ttp', 'ir', 'tt', 't', 'a', 'park', 'st', 'pa', 'r', 'ssa', 'r', 'r', 'a', 'ssa', 's', 'g', 'ssa', 'riting', 'p', 'ssa', 'r', 'sa', 'st', 'ssa', 'ing', 'sit', 'an', 'rit', 'an', 'ssa'] 

Example2:

>> print re.findall(u'[\u4E00-\u9FFF\u4E00-\u9FFF\uF900-\uFAFF]+', word_string) [] 

I've even tried using libraries like unicodedata or unicodescript to loop over characters and detect the language of each, but that gets really ugly really quickly.

Does anyone know a reliable way to check for CJK characters? Hopefully with some simple regex?

0

1 Answer 1

1

the word string should be unicode if you want the second example to work out.Eg

word_string = u"direct tv カルバンクライン 評価 カルバンクライン 価格 赤ジャージ アディダス nike エアリフト dg コート dg ネクタイ fresh 香水 フランス fresh 香水 ハワイ 1directtvcom ビームス バンズ コラボ httpwww1directtvcom 厚底コンバース 通販 フルラ バッグ 値段 プーマ専門店 ドクターマーチン ショート フルラ バッグ 新作 2015 スタッズグラディエーターサンダルモード・エ・ジャコモ mode et jacomo parker パーカー 万年筆 デュオフォールド" 

Tried in my cmd line.

Sign up to request clarification or add additional context in comments.

3 Comments

I just got [u'ir', u't', u't', u'nik', u'g', u'g', u'r', u's', u'r', u's', u'ir', u'tt', u'ttp', u'ir', u'tt', u't', u'a', u'park', u'r'] as the result. What version of python? I'm using 2.7 right now. Does this require python3?
2.6.6, default version of centos 6.5
Oh! Seems to work for example 2. word_string.decode('utf-8')! Boom!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.