最大匹配(默认):
最小匹配(加一个问号):
匹配 // 注释:
[\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")); } }
备注:替换非第一行和第一行
标签:file,java,String,read,注释,正则,贪婪,FileUtils From: https://www.cnblogs.com/zno2/p/17160627.html