Saturday, April 10, 2004

 

Regular Expression's Unicode support

之前po在個人版上的舊文章
那時因為shumi要幫某師大語文系的老師寫個統計中文字的小程式
所以順便就研究出來了~

如果想要 regxp 只對應到中文字
Pattern.compile()裡面的 parrten 就要是 \p{InBlock}
其中 Block 要參考下面連結
http://www.unicode.org/Public/3.0-Update/Blocks-3.txt

要對照 Greek 文字 就要寫 \p{InGreek}
如果 Block 裡面有空白,要把空白去掉
像是包含中日韓文的 CJK Unified Ideographs 就要寫成 \p{InCJKUnifiedIdeographs}

下面是程式碼:

Matcher matcher = Pattern.compile("\\p{InCJKUnifiedIdeographs}").matcher(字串);
while( matcher.find() ){
String 一個中文字 = matcher.group();
}

由 swanky 發表於 April 10, 2004 12:03 AM

Comments: Post a Comment



<< Home