代码之家  ›  专栏  ›  技术社区  ›  brovar

Java-在代码中查找注释的正则表达式

  •  16
  • brovar  · 技术社区  · 16 年前

    一点 享乐

    // some comment
    class Main {
        /* blah */
        // /* foo
        foo();
        // foo */
        foo2();
        /* // foo2 */
    }
    

    查找其中的所有注释并将其删除。我正在尝试使用正则表达式,目前我做了如下工作:

    private static String ParseCode(String pCode)
    {
        String MyCommentsRegex = "(?://.*)|(/\\*(?:.|[\\n\\r])*?\\*/)";
        return pCode.replaceAll(MyCommentsRegex, " ");
    }
    

    System.out.print("We can use /* comments */ inside a string of course, but it shouldn't start a comment");
    

    有什么与regex不同的建议或想法吗?

    5 回复  |  直到 16 年前
        1
  •  27
  •   Jacob Phillips    13 年前

    到现在为止,你可能已经放弃了,但我对这个问题很感兴趣。

    我相信这是一个局部解决方案。。。

    //.*|("(?:\\[^"]|\\"|.)*?")|(?s)/\*.*?\*/
    

    在Java中:

    String clean = original.replaceAll( "//.*|(\"(?:\\\\[^\"]|\\\\\"|.)*?\")|(?s)/\\*.*?\\*/", "$1 " );
    

    这似乎可以正确处理字符串中嵌入的注释以及字符串中正确转义的引号。我扔了一些东西来检查,但并没有穷尽。

    int/* some comment */foo = 5;
    

        2
  •  3
  •   tangens    16 年前

    最后一个例子没问题,我认为:

    /* we comment out some code
    System.out.print("We can use */ inside a string of course");
    we end the comment */
    

    ... 因为这个评论实际上以 "We can use */

    但我还有另一个问题:

    int/*comment*/foo=3;
    

    您的模式将此转换为:

    intfoo=3;
    

    " " 而不是 "" .

        3
  •  3
  •   alex    16 年前

    我认为使用正则表达式的100%正确的解决方案要么是不人道的,要么是不可能的(考虑到逃逸等)。

    我相信最好的选择是使用ANTLR——我相信他们甚至提供了一个您可以使用的Java语法。

        4
  •  3
  •   Ani Menon    10 年前

    我最终得到了这个解决方案。

    public class CommentsFun {
        static List<Match> commentMatches = new ArrayList<Match>();
    
        public static void main(String[] args) {
            Pattern commentsPattern = Pattern.compile("(//.*?$)|(/\\*.*?\\*/)", Pattern.MULTILINE | Pattern.DOTALL);
            Pattern stringsPattern = Pattern.compile("(\".*?(?<!\\\\)\")");
    
            String text = getTextFromFile("src/my/test/CommentsFun.java");
    
            Matcher commentsMatcher = commentsPattern.matcher(text);
            while (commentsMatcher.find()) {
                Match match = new Match();
                match.start = commentsMatcher.start();
                match.text = commentsMatcher.group();
                commentMatches.add(match);
            }
    
            List<Match> commentsToRemove = new ArrayList<Match>();
    
            Matcher stringsMatcher = stringsPattern.matcher(text);
            while (stringsMatcher.find()) {
                for (Match comment : commentMatches) {
                    if (comment.start > stringsMatcher.start() && comment.start < stringsMatcher.end())
                        commentsToRemove.add(comment);
                }
            }
            for (Match comment : commentsToRemove)
                commentMatches.remove(comment);
    
            for (Match comment : commentMatches)
                text = text.replace(comment.text, " ");
    
            System.out.println(text);
        }
    
        //Single-line
    
        // "String? Nope"
    
        /*
        * "This  is not String either"
        */
    
        //Complex */
        ///*More complex*/
    
        /*Single line, but */
    
        String moreFun = " /* comment? doubt that */";
    
        String evenMoreFun = " // comment? doubt that ";
    
        static class Match {
            int start;
            String text;
        }
    }
    
        5
  •  0
  •   Suraj Chandran    16 年前

    推荐文章