Regex that Will Match a Java Method Declaration

后端 未结 14 1174
半阙折子戏
半阙折子戏 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:29

    A tip:

    If you are going to write the regex in Perl, please use the "xms" options so that you can leave spaces and document the regex. For example you can write a regex like:

     m{\w+ \s+      #return type
       \w+ \s*      #function name
       [(] [^)]* [)] #params
       \s* [{]           #open paren
      }xms
    

    One of the options (think x) allows the # comments inside a regex. Also use \s instead of a " ". \s stands for any "blank" character. So tabs would also match -- which is what you would want. In Perl you don't need to use / /, you can use { } or < > or | |.

    Not sure if other languages have this ability. If they do, then please use them.

    0 讨论(0)
  • 2020-11-27 18:29

    I built a vim regex to do this for ctrlp/funky based on Georgios Gousios's answer.

        let regex = '\v^\s+'                " preamble
        let regex .= '%(<\w+>\s+){0,3}'     " visibility, static, final
        let regex .= '%(\w|[<>[\]])+\s+'    " return type
        let regex .= '\w+\s*'               " method name
        let regex .= '\([^\)]*\)'           " method parameters
        let regex .= '%(\w|\s|\{)+$'        " postamble
    

    I'd guess that looks like this in Java:

    ^\s+(?:<\w+>\s+){0,3}(?:[\w\<\>\[\]])+\s+\w+\s*\([^\)]*\)(?:\w|\s|\{)+$
    
    0 讨论(0)
  • (public|private|static|protected|abstract|native|synchronized) +([a-zA-Z0-9<>._?, ]+) +([a-zA-Z0-9_]+) *\\([a-zA-Z0-9<>\\[\\]._?, \n]*\\) *([a-zA-Z0-9_ ,\n]*) *\\{

    The Regex above will detect all possible java method definitions. Tested on lot's of source code files. To include constructors as well use the below regex :

    (public|private|static|protected|abstract|native|synchronized) +([a-zA-Z0-9<>._?, ]*) +([a-zA-Z0-9_]+) *\\([a-zA-Z0-9<>\\[\\]._?, \n]*\\) *([a-zA-Z0-9_ ,\n]*) *\\{

    0 讨论(0)
  • 2020-11-27 18:36

    I found seba229's answer useful, it captures most of the scenarios, but not the following,

    public <T> T name(final Class<T> x, final T y)
    

    This regex will capture that also.

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

    Hope this helps.

    0 讨论(0)
  • 2020-11-27 18:39

    This is for a more specific use case but it's so much simpler I believe its worth sharing. I did this for finding 'public static void' methods i.e. Play controller actions, and I did it from the Windows/Cygwin command line, using grep; see: https://stackoverflow.com/a/7167115/34806

    cat Foobar.java | grep -Pzo '(?s)public static void.*?\)\s+{'
    

    The last two entries from my output are as follows:

    public static void activeWorkEventStations (String type,
                String symbol,
                String section,
                String day,
                String priority,
                @As("yyyy-MM-dd") Date scheduleDepartureDate) {
    public static void getActiveScheduleChangeLogs(String type,
                String symbol,
                String section,
                String day,
                String priority,
                @As("yyyy-MM-dd") Date scheduleDepartureDate) {
    
    0 讨论(0)
  • 2020-11-27 18:40
    (public|private|static|protected) ([A-Za-z0-9<>.]+) ([A-Za-z0-9]+)\(
    

    Also, here's a replace sequence you can use in IntelliJ

    $1 $2 $3(
    

    I use it like this:

    $1 $2 aaa$3(
    

    when converting Java files to Kotlin to prevent functions that start with "get" from automatically turning into variables. Doesn't work with "default" access level, but I don't use that much myself.

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