I am trying to identify a java function block addition in a diff file (new line start with +
in diff).
For that I am using regular expression.
It is working fine, however I need to identify all (if not, at least maximum) possible combinations of keyword used in java method declarations.
Here is my regular expression written in my java program
Pattern functionPatter = Pattern.compile("\\+\\s*(public|private|protected)?(\\s+static)?(\\s+final)?\\s+\\w+\\s+\\w+\\s*\\(.*?\\)\\s*(throws .*)+\\{.*\\}",Pattern.DOTALL);
Please suggest me if I am missing anything.
2 Answers 2
I have no idea what you are trying to match with \\s+\\w+\\s+\\w+\\s*
. If you are looking for a return type, then that won't do at all.
It appears that you are trying to match the function body using \\{.*\\}
. That won't work, because braces may be nested, and regular expressions cannot handle nesting.
Modifier keywords can appear in any order, and you are missing a few, such as:
synchronized
transient
volatile
strictfp
If you want to implement this correctly, don't just make assumptions based on code that you have been seeing. Read the Java Language Specification.
For the java spec there are already complete parsers based e.g. on JavaCC or Antlr. IMHO the safest way would be process a java file using one of those and extracting the methods from there. Processing using regular expressions is quite difficult, e.g. how about methods in comments.
-
\$\begingroup\$ or methods as String value
String m = "public static void main()";
\$\endgroup\$Sharon Ben Asher– Sharon Ben Asher2017年09月25日 07:45:12 +00:00Commented Sep 25, 2017 at 7:45