从java中的源代码中删除注释

我想从 java 源代码文件中删除所有类型的注释语句。例子:


    String str1 = "SUM 10"      /*This is a Comments */ ;   

    String str2 = "SUM 10";     //This is a Comments"  

    String str3 = "http://google.com";   /*This is a Comments*/

    String str4 = "('file:///xghsghsh.html/')";  //Comments

    String str5 = "{\"temperature\": {\"type\"}}";  //comments

预期输出:


    String str1 = "SUM 10"; 

    String str2 = "SUM 10";  

    String str3 = "http://google.com";

    String str4 = "('file:///xghsghsh.html/')";

    String str5 = "{\"temperature\": {\"type\"}}";

我正在使用下面的正则表达式来实现:


    System.out.println(str1.replaceAll("[^:]//.*|/\\\\*((?!=*/)(?s:.))+\\\\*/", ""));

这给了我 str4 和 str5 的错误结果。请帮我解决这个问题。


使用 Andreas 解决方案:


        final String regex = "//.*|/\\*(?s:.*?)\\*/|(\"(?:(?<!\\\\)(?:\\\\\\\\)*\\\\\"|[^\\r\\n\"])*\")";

        final String string = "    String str1 = \"SUM 10\"      /*This is a Comments */ ;   \n"

             + "    String str2 = \"SUM 10\";     //This is a Comments\"  \n"

             + "    String str3 = \"http://google.com\";   /*This is a Comments*/\n"

             + "    String str4 = \"('file:///xghsghsh.html/')\";  //Comments\n"

             + "    String str5 = \"{\"temperature\": {\"type\"}}";  //comments";

        final String subst = "$1";


        // The substituted value will be contained in the result variable

        final String result = string.replaceAll(regex,subst);


        System.out.println("Substitution result: " + result);

它的工作除了 str5。


Qyouu
浏览 188回答 4
4回答

交互式爱情

要使其工作,您需要“跳过”字符串文字。您可以通过匹配字符串文字、捕获它们以便保留它们来做到这一点。以下正则表达式将执行此操作,用作$1替换字符串://.*|/\*(?s:.*?)\*/|("(?:(?<!\\)(?:\\\\)*\\"|[^\r\n"])*")有关演示,请参见regex101 。Java代码是:str1.replaceAll("//.*|/\\*(?s:.*?)\\*/|(\"(?:(?<!\\\\)(?:\\\\\\\\)*\\\\\"|[^\r\n\"])*\")", "$1")解释//.*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Match // and rest of line|&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; or/\*(?s:.*?)\*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Match /* and */, with any characters in-between, incl. linebreaks|&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; or("&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Start capture group and match "&nbsp; (?:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Start repeating group:&nbsp; &nbsp; &nbsp;(?<!\\)(?:\\\\)*\\"&nbsp; &nbsp; &nbsp;Match escaped " optionally prefixed by escaped \'s&nbsp; &nbsp; &nbsp;|&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; or&nbsp; &nbsp; &nbsp;[^\r\n"]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Match any character except " and linebreak&nbsp; )*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;End of repeating group")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Match terminating ", and end of capture group$1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Keep captured string literal

紫衣仙女

我推荐一个两步过程;一个基于行尾 (//),另一个不基于行尾 (/* */)。我喜欢帕维尔的想法;但是,我看不到它如何检查以确保星号是斜线后的下一个字符,反之亦然。我喜欢安德烈亚斯的想法;但是,我无法让它处理多行注释。https://docs.oracle.com/javase/specs/jls/se12/html/jls-3.html#jls-CommentTail

守着一只汪

正如其他人所说,正则表达式在这里不是一个好的选择。您可以使用简单的DFA来完成此任务。这是一个示例,它将为您提供多行注释 ( /* */) 的间隔。您可以对单行注释 ( // -- \n) 执行相同的方法。&nbsp; &nbsp; String input = ...; //here's your input String&nbsp; &nbsp; //0 - source code,&nbsp;&nbsp; &nbsp; //1 - multiple lines comment (start) (/ char)&nbsp; &nbsp; //2 - multiple lines comment (start) (* char)&nbsp; &nbsp; //3 - multiple lines comment (finish) (* char)&nbsp; &nbsp; //4 - multiple lines comment (finish) (/ char)&nbsp; &nbsp; byte state = 0;&nbsp;&nbsp; &nbsp; int startPos = -1;&nbsp; &nbsp; int endPos = -1;&nbsp; &nbsp; for (int i = 0; i < input.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; switch (state) {&nbsp; &nbsp; &nbsp; &nbsp; case 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (input.charAt(i) == '/') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;state = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;startPos = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (input.charAt(i) == '*') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state = 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (input.charAt(i) == '*') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;state = 3;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case 3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (input.charAt(i) == '/') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; state = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endPos = i+1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //here you have the comment between startPos and endPos indices,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //you can do whatever you want with it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

宝慕林4294392

也许,最好从多个简单的表达式开始,逐步进行,例如:.*(\s*\/\*.*|\s*\/\/.*)最初删除内联评论。演示测试import java.util.regex.Matcher;import java.util.regex.Pattern;final String regex = "(.*)(\\s*\\/\\*.*|\\s*\\/\\/.*)";final String string = "&nbsp; &nbsp; String str1 = \"SUM 10\"&nbsp; &nbsp; &nbsp; /*This is a Comments */ ;&nbsp; &nbsp;\n"&nbsp; &nbsp; &nbsp;+ "&nbsp; &nbsp; String str2 = \"SUM 10\";&nbsp; &nbsp; &nbsp;//This is a Comments\"&nbsp; \n"&nbsp; &nbsp; &nbsp;+ "&nbsp; &nbsp; String str3 = \"http://google.com\";&nbsp; &nbsp;/*This is a Comments*/\n"&nbsp; &nbsp; &nbsp;+ "&nbsp; &nbsp; String str4 = \"('file:///xghsghsh.html/')\";&nbsp; //Comments\n"&nbsp; &nbsp; &nbsp;+ "&nbsp; &nbsp; String str5 = \"{\\\"temperature\\\": {\\\"type\\\"}}\";&nbsp; //comments";final String subst = "\\1";final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);final Matcher matcher = pattern.matcher(string);// The substituted value will be contained in the result variablefinal String result = matcher.replaceAll(subst);System.out.println("Substitution result: " + result);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java