Comment detail
コメントの削除 (Nested Flatten)文字列リテラルに対応していませんでした。あわてると駄目ですね。
文字列リテラル対応版です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.util.regex.*;
import java.io.*;
public class Decomment {
static final String LITERAL = "\"(?:\\\\.|[^\"])*\"";
static final String COMMENT1 = "(?s:/\\*.*?\\*/)";
static final String COMMENT2 = "//.*";
static final Pattern DECOM_PAT = Pattern.compile("(" + LITERAL + ")|" +
COMMENT1 + "|" + COMMENT2);
static final String LINE_SEPARATOR = System.getProperty("line.separator");
public static void main(String[] args) throws IOException {
for (String name : args) {
BufferedReader r = new BufferedReader(new FileReader(name));
FileWriter w = new FileWriter(name + ".out");
StringBuilder sb = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
sb.append(line).append(LINE_SEPARATOR);
}
String result = DECOM_PAT.matcher(sb).replaceAll("$1");
w.write(result);
w.close();
}
}
}
|





匿名
#6544()
[
Java
]
Rating0/0=0.00
一番のりを狙って書きました。起動パラメータに与えられたJavaのソースファイルからコメントを削除し、".out" 拡張子つきのファイルに書き込みます。
Rating0/0=0.00-0+
1 reply [ reply ]