用于匹配多行嵌套注释的java正则表达式

时间:2015-10-04 16:22:40

标签: java regex annotations

我正在尝试编写一个java正则表达式来删除我的代码中的所有注释,我有一些非常复杂的注释嵌套,到目前为止我只能匹配内部注释,这里是我的注释的例子

@annotationA(property1 = "",
        property2 = "",
        property3 = "",
        property4 = "",
        property5 = "")
public class ClassA {

    @annotationB(property1 = @annotationA(property5 = "anyChar",
            property6 = false,
            property1 = "anyChar",
            property2 = "anyChar",
            property3 = "anyChar",
            property4 = "anyChar"),
            params = { @annotationC(property7 = @annotationA(property5 = "anyChar"), property8 = @annotationA(property5 = "anyChar")),
                    @annotationC(property7 = @annotationA(property5 = "anyChar"), property8 = @annotationA(property5 = "anyChar")),
                    @annotationC(property7 = @annotationA(property5 = "anyChar"), property8 = @annotationA(property5 = "anyChar")),
                    @annotationC(property7 = @annotationA(property5 = "anyChar"), property8 = @annotationA(property5 = "anyChar")),
                    @annotationC(property7 = @annotationA(property5 = "anyChar"), property8 = @annotationA(property5 = "anyChar")),
                    @annotationC(property7 = @annotationA(property5 = "anyChar"), property8 = @annotationA(property5 = "anyChar")),
                    @annotationC(property7 = @annotationA(property5 = "anyChar"), property8 = @annotationA(property5 = "anyChar")),
                    @annotationC(property7 = @annotationA(property5 = "anyChar"), property8 = @annotationA(property5 = "anyChar")),
                    @annotationC(property7 = @annotationA(property5 = "anyChar"), property8 = @annotationA(property5 = "anyChar")), },
            returnType = @annotationA(property5 = "anyChar"))
    //some methode

}

这是我的正则表达式(未转义):

@\w+\([\n\w\s=\-"\@,.*:// {\+ }\.;+]+\)

1 个答案:

答案 0 :(得分:1)

如果你试图删除注释,并且你有一个可以找到非嵌套的正则表达式,你基本上就完成了。除非它是一个对性能敏感的任务,否则你可以反复迭代该正则表达式,每次都删除它匹配的内容。删除内部注释后,将不再使用嵌套的注释。当正则表达式无法匹配时你就完成了这个过程!

您的示例(在Notepad ++中)完全清理了3个replaceAlls,看起来可以接受。