Regex that Will Match a Java Method Declaration

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

    I came up with this:

    \b\w*\s*\w*\(.*?\)\s*\{[\x21-\x7E\s]*\}
    

    I tested it against a PHP function but it should work just the same, this is the snippet of code I used:

    function getProfilePic($url)
     {
        if(@open_image($url) !== FALSE)
         {
            @imagepng($image, 'images/profiles/' . $_SESSION['id'] . '.png');
            @imagedestroy($image);
            return TRUE;
         }
        else 
         {
            return FALSE;
         }
     }
    

    MORE INFO:

    Options: case insensitive
    
    Assert position at a word boundary «\b»
    Match a single character that is a “word character” (letters, digits, etc.) «\w*»
       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
    Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) «\s*»
       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
    Match a single character that is a “word character” (letters, digits, etc.) «\w*»
       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
    Match the character “(” literally «\(»
    Match any single character that is not a line break character «.*?»
       Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
    Match the character “)” literally «\)»
    Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.) «\s*»
       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
    Match the character “{” literally «\{»
    Match a single character present in the list below «[\x21-\x7E\s]*»
       Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
       A character in the range between ASCII character 0x21 (33 decimal) and ASCII character 0x7E (126 decimal) «\x21-\x7E»
       A whitespace character (spaces, tabs, line breaks, etc.) «\s»
    Match the character “}” literally «\}»
    
    
    Created with RegexBuddy
    

提交回复
热议问题