0

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=\-"\@,.*:// {\+ }\.;+]+\)
1
  • Regular expressions are not the right tool to parse java language. You may consider using a parser generator like antlr, although the learning curve might be steep. You can find ready-made grammar for java language at github.com/antlr/grammars-v4/blob/master/java/Java.g4 Commented Oct 4, 2015 at 16:44

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.