トップページに戻る
   次の正規表現パズルへ
   前の正規表現パズルへ
10-6 文字クラスの集合演算(差集合)
正規表現パズル
ソース
import java.util.regex.*;
public class sample10_6{
    private static final String PATTERN = "[a-r&&[^g-x]]+";
    public static void main(String argv[]){
        String[] strArray = new String[5+1];
        strArray[1] = "abcdef";
        strArray[2] = "ghijkl";
        strArray[3] = "mnopqr";
        strArray[4] = "stuvwx";
        strArray[5] = "yz";
        System.out.println("Pattern " + PATTERN);
        Pattern p = Pattern.compile(PATTERN);
        for (int i=1;i<=5;i++){
            String willOut = "Line" + Integer.toString(i) + " " + strArray[i] + " ";
            Matcher m = p.matcher(strArray[i]);
            if (m.find()) willOut += "matchs " + m.group();
            else willOut += "no match";
            System.out.println(willOut);
        }
    }
}
実行結果
Pattern [a-r&&[^g-x]]+
Line1 abcdef matchs abcdef
Line2 ghijkl no match
Line3 mnopqr no match
Line4 stuvwx no match
Line5 yz no match