トップページに戻る
次の正規表現パズルへ
前の正規表現パズルへ
10-3 強欲な量指定子
正規表現パズル
ソース
import java.util.regex.*;
public class sample10_3{
private static final String PATTERN = "^[ab]*+[bc]+$";
public static void main(String argv[]){
String[] strArray = new String[4+1];
strArray[1] = "aaabbb";
strArray[2] = "aaabbbccc";
strArray[3] = "bbbccc";
strArray[4] = "ccc";
System.out.println("Pattern " + PATTERN);
Pattern p = Pattern.compile(PATTERN);
for (int i=1;i<=4;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 ^[ab]*+[bc]+$
Line1 aaabbb no match
Line2 aaabbbccc matchs aaabbbccc
Line3 bbbccc matchs bbbccc
Line4 ccc matchs ccc