Need to parse a Class declaration line in Java using regular expression e.g.
String line = "public class ActionDiagramReader extends XReader implements ActionHandler, Action {";
String line = "public class ActionDiagramReader extends XReader{";
I am trying to use this regex
String regExp = ".*class\\s+(\\w+)(\\s+extends\\s+(\\w+))?(\\s+implements\\s+(\\w|,)+)?\\{.*$";
Pattern pattern = Pattern.compile(regExp, Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
Matcher match = pattern.matcher(line1);
But this regex fails to parse classname/extender classname/implementer clas-name. Need to just identify all the classes & interfaces associated with given lines.
What is incorrect in my regular expression.
Thanks in advance
The last part in your regex isn’t grabbing the action.
It grabs up to ActionHandler, then dies because it’s looking for curly brace next.
It my example below, I put the final \w into square braces and added \s to also capture any spaces, this way it will capture the “Action” at the end of the line as well.
See if that works for you.