简体   繁体   中英

java regex for matching multiline nested annotation

i'm trying to write a java regular expression to remove all the annotation from my code ,i have some pretty complex annotation that are nested and thus far i could only match on the inner annotations , here is an exemple of my annotations

@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

}

and here is my regex (not escaped):

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

If you're trying to remove the annotations, and you have a regex that will find non-nested ones you are basically done. Unless it's a performance-sensitive task, you can just iterate that regex over and over again, each time deleting what it matches. After inner annotations are deleted there will no longer be a nested one to take care of. End the process when the regex can't match anything and you're done!

Your example (in Notepad++) took 3 replaceAlls to clean completely, which looks acceptable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM