トップページに戻る
次の正規表現パズルへ
前の正規表現パズルへ
10-4 文字クラスの集合演算(和集合)
正規表現パズル
ソース
import java.util.regex.*;
public class sample10_4{
private static final String PATTERN = "[a-f[m-r]]+";
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-f[m-r]]+
Line1 abcdef matchs abcdef
Line2 ghijkl no match
Line3 mnopqr matchs mnopqr
Line4 stuvwx no match
Line5 yz no match