zno2

正则非贪婪 (清除java类三种注释)

最大匹配(默认):

 

 

最小匹配(加一个问号):

 

 

 

匹配 // 注释:

[\t  ]*//.*[^\r\n]

匹配 /** */ doc注释和/* */ 块注释():

[\t  ]*/\*[\s\S]*?\*/

 

验证:

/*
 * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.

 */
package zz;

/**
 * @author witas
 *
 */
public class Woo {

    // sdfsdf
    // xcvxcv
    // ghfghfgh
    /**
     * @param args
     */
    public static void main(String[] args) {
        String s = "//sdfsdf";
        String s2 = "/** */";
    }
    
    /* sdfsdfsdf */
    
    
}

 

    public static void cleanComment(File base) {
        File[] search = FileUtils.search("*.java", base);
        for (File file : search) {
            String read = FileUtils.read(file, Charset.forName("utf-8"));
            read = read.replaceAll("\\r\\n[\\t  ]*//.*[^\\r\\n]", "\r\n").replaceFirst("^[\\t  ]*//.*[^\\r\\n]", "");
            read = read.replaceAll("\\r\\n[\\t  ]*/\\*[\\s\\S]*?\\*/", "\r\n").replaceFirst("^[\\t  ]*/\\*[\\s\\S]*?\\*/", "");
            FileUtils.write(read, file, Charset.forName("utf-8"));
        }
    }

备注:替换非第一行和第一行

posted on 2023-08-03 19:51  zno2  阅读(10)  评论(0编辑  收藏  举报

导航