用正则表达式可以做到。 示例: import java.util.regex.*; public class ReFlags { /* *查找注释行 */ public static void main(String[] args) { Pattern p=Pattern.compile("((?m)//.*$)|(/\\*([^/\\*]*)\\*/)"); Matcher m=p.matcher( "//hi,everyone ni hao\n" +"/*"+ "JAVA has pretty good regular expressions\n" + "Regular expressions are in java"+ "*/"+"/*"+ "everyone is happy."+ "i love you?\n" + "you love me"+ "*/"+"//hi,everyone ni hao\n" ); while(m.find()){ System.out.println(m.group()); } } }使用String.replaceAll(正则表达式,替换后的字符串)可以替换。
示例:
import java.util.regex.*;
public class ReFlags {
/*
*查找注释行
*/
public static void main(String[] args) {
Pattern p=Pattern.compile("((?m)//.*$)|(/\\*([^/\\*]*)\\*/)");
Matcher m=p.matcher(
"//hi,everyone ni hao\n" +"/*"+
"JAVA has pretty good regular expressions\n" +
"Regular expressions are in java"+
"*/"+"/*"+
"everyone is happy."+
"i love you?\n" +
"you love me"+
"*/"+"//hi,everyone ni hao\n"
);
while(m.find()){
System.out.println(m.group());
}
}
}使用String.replaceAll(正则表达式,替换后的字符串)可以替换。
问题还没有解决,我想在shell里面控制,有没有只在shell就可以搞定的啊?