概要
Javaで「UNICODE_CASEフラグ(Unicode準拠の文字でも大文字と小文字を区別しない)」の動作を確認してみました。
以下のページを参考にしました。
実装
以下のファイルを作成しました。
JSample22_1.java
import java.util.regex.*;
class JSample22_1{
public static void main(String[] args){
String target1 = "book";
String target2 = "BOOK";
String regex = "book";
Pattern p1 = Pattern.compile(regex);
Matcher m1_1 = p1.matcher(target1);
System.out.println(target1 + ":" + m1_1.find());
Matcher m1_2 = p1.matcher(target2);
System.out.println(target2 + ":" + m1_2.find());
System.out.println("---- ----");
Pattern p2 =
Pattern.compile(regex, Pattern.CASE_INSENSITIVE|Pattern.UNICODE_CASE);
Matcher m2_1 = p2.matcher(target1);
System.out.println(target1 + ":" + m2_1.find());
Matcher m2_2 = p2.matcher(target2);
System.out.println(target2 + ":" + m2_2.find());
}
}
以下のコマンドを実行しました。
$ javac JSample22_1.java
$ java JSample22_1
book:true
BOOK:false
---- ----
book:true
BOOK:true
まとめ
何かの役に立てばと。