Description
(?<=\n|\A)(?:public\s)?(class|interface|enum)\s([^\n\s]*)

This regex does the following:
- allow the string to start with
public or not
- be a
class or interface or enum
- capture the name
Note, I recommend using the global and case insensitive flags
Example
Live Example
https://regex101.com/r/vR0iK3/1
Sample Text
/**
*
* @author XXXX
* Introduction: A common interface that judges all kinds of algorithm tags.
* some other comment
*/
public class TagMatchingInterface
{
// content
public class InnerClazz{
// content
}
}
Sample Matches
[0][0] = public class TagMatchingInterface
[0][1] = class
[0][2] = TagMatchingInterface
Capture groups:
- group 0 gets the entire match
- group 1 gets the class
- group 2 gets the name
Explanation
NODE EXPLANATION
----------------------------------------------------------------------
(?<= look behind to see if there is:
----------------------------------------------------------------------
\n '\n' (newline)
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
\A Start of the string
----------------------------------------------------------------------
) end of look-behind
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
public 'public'
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
class 'class'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
interface 'interface'
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
enum 'enum'
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
\s whitespace (\n, \r, \t, \f, and " ")
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
[^\n\s]* any character except: '\n' (newline),
whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------