English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaのパターンのUNICODE_CHARACTER_CLASSフィールドと例

UnicodeバージョンのプレデファインドキャラクタークラスとPOSIXキャラクタークラスを有効にします。

import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UNICODE_CHARACTER_CLASS_Example {
   public static void main( String args[] ) {
      String regex = "\u00de";
      //正規表現をコンパイル
      Pattern pattern = Pattern.compile(regex, Pattern.UNICODE_CHARACTER_CLASS);
      //検索マッチャーオブジェクト
      String str[] = {"\u00de", "\u00fe", "\u00ee", "\u00ce"};
      for (String ele : str) {
         Matcher matcher = pattern.matcher(ele);
         if(matcher.matches()) {
            System.out.println(ele+" is a match for "+regex);
         } else {
            System.out.println(ele+" is not a match for "+regex);
         }
      }
   }
}

出力結果

Þ is a match for Þ
þ is a match for Þ
î is not a match for Þ
Î is not a match for Þ