Regex that Will Match a Java Method Declaration

后端 未结 14 1176
半阙折子戏
半阙折子戏 2020-11-27 17:53

I need a Regex that will match a java method declaration. I have come up with one that will match a method declaration, but it requires the opening bracket of the method to

相关标签:
14条回答
  • 2020-11-27 18:51

    I also needed such a regular expression and came up with this solution:

    (?:(?:public|private|protected|static|final|native|synchronized|abstract|transient)+\s+)+[$_\w<>\[\]\s]*\s+[\$_\w]+\([^\)]*\)?\s*\{?[^\}]*\}?
    

    This grammar and Georgios Gousios answer have been useful to build the regex.

    EDIT: Considered tharindu_DG's feedback, made groups non-capturing, improved formatting.

    0 讨论(0)
  • 2020-11-27 18:53
    (public|protected|private|static|\s) +[\w\<\>\[\]]+\s+(\w+) *\([^\)]*\) *(\{?|[^;])
    

    I think that the above regexp can match almost all possible combinations of Java method declarations, even those including generics and arrays are return arguments, which the regexp provided by the original author did not match.

    0 讨论(0)
提交回复
热议问题