トップページに戻る
次の正規表現パズルへ
前の正規表現パズルへ
10-7 ?を使う可変長戻り読み
正規表現パズル
ソース
import java.util.regex.*;
public class sample10_7{
private static final String PATTERN = "(?<=Xa?b?c?)Y.*$";
public static void main(String argv[]){
String[] strArray = new String[7+1];
strArray[1] = "XX111";
strArray[2] = "XabcY222";
strArray[3] = "XabY333";
strArray[4] = "XaY444";
strArray[5] = "XacY555";
strArray[6] = "XccY666";
strArray[7] = "XY777";
System.out.println("Pattern " + PATTERN);
Pattern p = Pattern.compile(PATTERN);
for (int i=1;i<=7;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 (?<=Xa?b?c?)Y.*$
Line1 XX111 no match
Line2 XabcY222 matchs Y222
Line3 XabY333 matchs Y333
Line4 XaY444 matchs Y444
Line5 XacY555 matchs Y555
Line6 XccY666 no match
Line7 XY777 matchs Y777