正则表达式

action_520_12 2008-01-10 02:18:26
紧急求救!!!
我想把java文件中的注释都去掉,比如类库中的File.java,正则表达式该如何写?
最好附有源码。
多谢DX了!!
...全文
187 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
(?s)是匹配模式的嵌入式代码,即Pattern.DOTALL,由于replaceAll不能放入匹配模式所以只能采用嵌入的。
weijiepeng 2008-01-10
  • 打赏
  • 举报
回复
("(?s)/\\*.*?\\*/", "")
(?s)是什么意思
  • 打赏
  • 举报
回复
哈哈,去看看“老紫竹”的一篇文章吧,里面详细地介绍了如何结帖

http://www.java2000.net/viewthread.jsp?tid=209

分数与给分不符,是由于这张帖子你放了20分(也就是楼主那里的“问题点数”),所以加起来的分数只能是20分。
action_520_12 2008-01-10
  • 打赏
  • 举报
回复
结贴的时候总提示:

分数总和与帖子给分不相符

嘛原因呐?
action_520_12 2008-01-10
  • 打赏
  • 举报
回复
呵呵,还是火龙果厉害。。。

我刚调试成功!

多谢各位!!

PS:还不会派分,请教。。。
  • 打赏
  • 举报
回复
如果你只是要把 /* */的替换掉,可以参考下面的:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Test {
public static void main(String[] args) {
File file = new File("D:/Green/jdk1.5src/java/io/File.java");
String str = readFile2String(file);
str = str.replaceAll("(?s)/\\*.*?\\*/", "");
System.out.println(str);
}

private static String readFile2String(File file) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line = System.getProperty("line.separator");
try {
br = new BufferedReader(new FileReader(file));
String str = null;
while((str = br.readLine()) != null) {
sb.append(str).append(line);
}
}catch(IOException e){
e.printStackTrace();
}finally{
try {
br.close();
}catch(IOException e){
e.printStackTrace();
}
}
return sb.toString();
}
}
healer_kx 2008-01-10
  • 打赏
  • 举报
回复
用火龙果的代码多好啊,肯定比正则要快一些的。
  • 打赏
  • 举报
回复
你试试我在5楼帖出的代码,Java文件中字符串内不包括“//”“/*”的基本上都可以用的。

如果处理的文件中字符串内包括上面的那个字符,还就更加麻烦了,还需要更进一步地处理。
action_520_12 2008-01-10
  • 打赏
  • 举报
回复
而且这个正则表达式肯定要用到懒惰,要不正常的代码都要被替换掉。
action_520_12 2008-01-10
  • 打赏
  • 举报
回复
现在只要处理/* */这样的注释就可以了,还是可以用正则表达式的。
我用一小段注释试过,可以匹配,但是应用到整个java文件时就不行了。
believefym 2008-01-10
  • 打赏
  • 举报
回复
这个不是那么好写的吧,
涉及到语法问题
  • 打赏
  • 举报
回复
Java中的注释是多种多样的,不能采用正则表达式,太麻烦了,以前写过一个,贴出来你参考一下:

测试用的Java注释文件:
// Test File
package color;

/**
* My firat Java program: Hello World
* @author bao110908
*/
public class Test{

/*
main method
method of automatic startup
*/

/* abnormal comment 1 */
/** abnormal comment 2 **/

// normal comment
public static void main(String[] args) {
System.out.println("Hello World"); // print 'Hello World'
System.out.println("Hello World"); /* abnormal comment 3 */
}
}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Test {
public static void main(String[] args) {
Comment comment = new Comment();
int count = comment.countCommentLine("abc.java");
System.out.println("总共的注释行数:" + count);

System.out.println();
List<String> codes = comment.removeComment("abc.java");
System.out.println("没有注释的源代码");
for (String str : codes) {
System.out.println(str);
}
}
}

class Comment {

/**
* 计算某个 Java 文件中注释的行数
*/
public int countCommentLine(String filename) {
if (filename.toLowerCase().lastIndexOf(".java") <= 0) {
throw new IllegalJavaFile(filename);
}
int count = 0;
List<String> list = Util.readFile(filename);
boolean isMultiLineComment = false;
for (String str : list) {
// 当为多行注释时,直接将计算注释行
if (isMultiLineComment) {
count++;
// 直到碰到 */ 为止,当然 */ 这一行也算注释
if (str.indexOf("*/") >= 0) {
isMultiLineComment = false;
}
}
// 为 // 直接计数
if (str.indexOf("//") >= 0) {
count++;
}

// 当碰到 /* 时,开始计数
if (str.indexOf("/*") >= 0) {
count++;
// 并且在该行中没 */ 时,可以看作是多行注释的开始
if (str.indexOf("*/") < 0) {
isMultiLineComment = true;
}
}
}
return count;
}

/**
* 除去文件中的注释,与 countCommentLine 方法类似
*/
public List<String> removeComment(String filename) {
if (filename.toLowerCase().lastIndexOf(".java") <= 0) {
throw new IllegalJavaFile(filename);
}
List<String> list = Util.readFile(filename);
List<String> codes = new ArrayList<String>();
boolean isMultiLineComment = false;
for (int i = 0, k = list.size(); i < k; i++) {
String code = list.get(i);

if (isMultiLineComment) {
if (code.indexOf("*/") >= 0) {
isMultiLineComment = false;
}
continue;
}

int index = code.indexOf("/*");
if (index >= 0) {
String s = code.substring(0, index);
if (s.trim().length() > 0) {
codes.add(s);
}
if (code.indexOf("*/") < 0) {
isMultiLineComment = true;
}
continue;
}

index = code.indexOf("//");
if (index >= 0) {
String s = code.substring(0, index);
if (s.trim().length() > 0) {
codes.add(s);
}
continue;
}
codes.add(code);
}
return codes;
}
}

class IllegalJavaFile extends RuntimeException {
private static final long serialVersionUID = 1L;

public IllegalJavaFile(String filename) {
super("\"" + filename + "\", the file is not a java file.");
}
}

class Util {
public static List<String> readFile(String filename) {
List<String> list = new ArrayList<String>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filename));
String str = "";
while ((str = br.readLine()) != null) {
// 忽略空行
if (str.trim().length() > 0) {
list.add(str);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}

public static void writeFile(List<String> codes, String filename) {
BufferedWriter br = null;
try {
br = new BufferedWriter(new FileWriter(filename));
String line = System.getProperty("line.separator");
for (int i = 0, k = codes.size(); i < k; i++) {
br.write(codes.get(i));
br.write(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
healer_kx 2008-01-10
  • 打赏
  • 举报
回复
你没有处理//这样的注释吧。?

/*
*/
这个要注意注释的语法,否则你可以要删错代码。

我正则学得不好,我都是手工代码来处理这种东西。

action_520_12 2008-01-10
  • 打赏
  • 举报
回复
帮帮忙吧。。。
healer_kx 2008-01-10
  • 打赏
  • 举报
回复
这不是就快对了嘛,
action_520_12 2008-01-10
  • 打赏
  • 举报
回复

void fileCopy1(File src, File dest) {
BufferedInputStream bis;
BufferedOutputStream bos;
Pattern p = Pattern.compile("(\\/\\**\\s*\\n*([.&&[^/]])*?\\**\\/)");
byte[] b = new byte[1024];
try {
bis = new BufferedInputStream(new FileInputStream(src));
int len = 0;
StringBuilder sb = new StringBuilder();
while ((len = bis.read(b)) != -1) {
sb.append(new String(b, 0, len));
}
bos = new BufferedOutputStream(new FileOutputStream(dest));
String str = sb.toString();
Matcher m = p.matcher(str);
while(m.find()) {
System.out.println("find");
m.replaceAll("");
}
bos.write(str.getBytes());
} catch (FileNotFoundException fnfex) {
System.out.println(fnfex.getMessage());
} catch (IOException ioex) {
System.out.println(ioex.getMessage());
} catch(Exception ex) {
System.out.println("message ======" + ex.getMessage());
}
}

我写的代码,可是不正确。
等待中。。。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧