/* 語頭のスペース文字等をトリミングする */
String trimHead(StringBuffer buf){
char ch;
int offset = 0;
while ((ch = buf.charAt(offset)) == ' '
|| ch == '・'){ //'・'はUnicodeではKATAKANAなのだ!
++offset;
}
return (buf.substring(offset));
}
/* 語頭のスペース文字等をトリミングする */
String trimHead(StringBuffer buf){
char ch;
int offset = 0;
/*必ず最初にインデクス変数の値をチェックすること!*/
while ((offset < buf.length()) && ((ch = buf.charAt(offset)) == ' '
|| ch == '・')){ //'・'はUnicodeではKATAKANAなのだ!
++offset;
}
return (buf.substring(offset));
}
/***************************************/
/* splittest.java */
import java.util.regex.*;
public class splittest{
public static void main(String[] args){
//Vimなどで使われるマッチ結果参照置換文字列
String target = "\\2abc\\51def\\2ghi\\1\\3";
String regx = ""; //このリテラルを正しく書いてください
String[] results;
System.out.println("\\2".matches(regx)); //regxが正しければtrue
System.out.println("-----------");
results = target.split(regx);
for (int i = 0; i < results.length; ++i){
System.out.println(results[i]);
}
System.out.println("-----------");
Pattern pat = Pattern.compile(regx);
Matcher mat = pat.matcher(target);
while (mat.find()){
System.out.println(mat.group());
}
}
}
/***************************************/
PrintWriter pw = new PrintWriter
(new OutputStreamWriter
(new FileOutputStream("testoutput.txt"), "UTF-8"));
BufferedReader br = new BufferedReader
(new InputStreamReader
(new FileInputStream("testinput.txt"), "UTF-8"));
ところが、こういう場合のOutputStreamWriterのコンストラクタへの第一引数として、PrintStream、典型的にはSystem.outを渡すと、文字コードが正しく処理されません。現在の私の単なる想像では、JDK 1.1以降のPrintStreamは、ネイティブの文字コードとの結びつきが特別に強くされているからではないか、と思われます。
if (.....);{
...
break;
}
if節のしっぽに間違って;があると、このif節の制御対象文は;(すなわち空文)になってしまい、{... break;}のほうはループの初回で無条件で実行されてしまいます。つまりループが初回でbreakしてしまいます。
DefaultTreeCellRenderer dtcr
= (DefaultTreeCellRenderer)(myTree.getCellRenderer()); //レンダラを取得
dtcr.setLeafIcon(new ImageIcon("my_gazou1.gif")); //リーフノード
dtcr.setClosedIcon(new ImageIcon("my_gazou2.gif")); //サブツリー(閉じ)
dtcr.setOpenIcon(new ImageIcon("my_gazou3.gif")); //サブツリー(開き)
そこで、画才のあるかたは、天皇家の家系図(または家系図一般)にふさわしいアイコン画像を作って、131ページのJomeiプログラムに加えてみてください。
bar\d(?=poo)
は、直後にpooのあるbar\dを指定する、と説明しています。この説明そのものは正しいのですが、lookaheadに関するもうひとつの重要な説明がなされていません。
X(?=Y) ... Yが直後にあるX
(?=Y)X ... Yの冒頭部分のX
となります。
John(?= Lennon)
は、「直後に" Lennon"のある"John"」ですからJohn LennonのJohnにマッチしますが、これもまた:
(?=John Lennon)John
「現在位置(ここでは行頭)の前方に"John Lennon"があり、その"John Lennon"の冒頭部分の"John"」という指定となり、やはりJohn LennonのJohnにマッチするのです。しかしこう書くと:
(?= Lennon)John
「前方に" Lennon"があってその冒頭部分の"John"」という、ナンセンスな指定になります。