用于检测类/接口/ etc声明的Java Regular Expression

时间:2014-10-22 12:24:55

标签: java regex

我试图创建一个检测新类的正则表达式,例如:

public interface IGame {

private class Game {

这是我到目前为止所做的,但它没有检测到:

(line.matches("(public|protected|private|static|\\s)"+"(class|interface|\\s)"+"(\\w+)"))

有人可以给我一些指示吗?

2 个答案:

答案 0 :(得分:5)

此正则表达式是Java类和接口声明的不完整规范。但是,它可以匹配如下声明:

  

abstract class X<B extends Integer,D extends java.io.InputStream,R extends Comparator<? super D>>extends java.util.ArrayList<Integer>implements java.util.Queue<Integer>,Serializable{}

     

public@Deprecated interface K<D> extends Comparable<Integer>{}

所以它应该足以满足大多数目的。

请阅读代码以在最后生成正则表达式,以确切知道跳过的内容。总而言之,ReferenceTypeAnnotation并未完全合并到此正则表达式中,因为它们需要递归正则表达式才能正确解析。

Java正则表达式在其所有g(l)ory细节中匹配Java类声明:

  

(?:((?:public|protected|private|abstract|static|final|strictfp|@[ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)(?:[ \t\f\r\n]*+(?:(?<!\p{javaJavaIdentifierPart})|(?!\p{javaJavaIdentifierPart}))(?:public|protected|private|abstract|static|final|strictfp|@[ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+))*+)[ \t\f\r\n]*+)?+(?:(?<!\p{javaJavaIdentifierPart})|(?!\p{javaJavaIdentifierPart}))class[ \t\f\r\n]++(\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)[ \t\f\r\n]*+(?:(<[ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]++extends[ \t\f\r\n]++(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]*+[.][ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)*+(?:[ \t\f\r\n]*+<[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+)(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+))*+[ \t\f\r\n]*+>)?+(?:[ \t\f\r\n]*+[.][ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]*+<[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+)(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+))*+[ \t\f\r\n]*+>)?+)*+|\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+))?+(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]++extends[ \t\f\r\n]++(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]*+[.][ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)*+(?:[ \t\f\r\n]*+<[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+)(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+))*+[ \t\f\r\n]*+>)?+(?:[ \t\f\r\n]*+[.][ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]*+<[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+)(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+))*+[ \t\f\r\n]*+>)?+)*+|\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+))?+)*+[ \t\f\r\n]*+>)[ \t\f\r\n]*+)?+(?:(?<!\p{javaJavaIdentifierPart})|(?!\p{javaJavaIdentifierPart}))(?:(extends[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]*+[.][ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)*+(?:[ \t\f\r\n]*+<[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+)(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+))*+[ \t\f\r\n]*+>)?+(?:[ \t\f\r\n]*+[.][ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]*+<[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+)(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+))*+[ \t\f\r\n]*+>)?+)*+)[ \t\f\r\n]*+)?+(?:(?<!\p{javaJavaIdentifierPart})|(?!\p{javaJavaIdentifierPart}))(?:(implements[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]*+[.][ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)*+(?:[ \t\f\r\n]*+<[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+)(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+))*+[ \t\f\r\n]*+>)?+(?:[ \t\f\r\n]*+[.][ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]*+<[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+)(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+))*+[ \t\f\r\n]*+>)?+)*+(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]*+[.][ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)*+(?:[ \t\f\r\n]*+<[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+)(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+))*+[ \t\f\r\n]*+>)?+(?:[ \t\f\r\n]*+[.][ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]*+<[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+)(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+))*+[ \t\f\r\n]*+>)?+)*+)*+[ \t\f\r\n]*+))?+[{]

对于Java接口声明:

  

(?:((?:public|protected|private|abstract|static|strictfp|@[ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)(?:[ \t\f\r\n]*+(?:(?<!\p{javaJavaIdentifierPart})|(?!\p{javaJavaIdentifierPart}))(?:public|protected|private|abstract|static|strictfp|@[ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+))*+)[ \t\f\r\n]*+)?+(?:(?<!\p{javaJavaIdentifierPart})|(?!\p{javaJavaIdentifierPart}))interface[ \t\f\r\n]++(\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)[ \t\f\r\n]*+(?:(<[ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]++extends[ \t\f\r\n]++(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]*+[.][ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)*+(?:[ \t\f\r\n]*+<[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+)(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+))*+[ \t\f\r\n]*+>)?+(?:[ \t\f\r\n]*+[.][ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]*+<[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+)(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+))*+[ \t\f\r\n]*+>)?+)*+|\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+))?+(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]++extends[ \t\f\r\n]++(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]*+[.][ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)*+(?:[ \t\f\r\n]*+<[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+)(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+))*+[ \t\f\r\n]*+>)?+(?:[ \t\f\r\n]*+[.][ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]*+<[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+)(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+))*+[ \t\f\r\n]*+>)?+)*+|\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+))?+)*+[ \t\f\r\n]*+>)[ \t\f\r\n]*+)?+(?:(?<!\p{javaJavaIdentifierPart})|(?!\p{javaJavaIdentifierPart}))(?:(extends[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]*+[.][ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)*+(?:[ \t\f\r\n]*+<[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+)(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+))*+[ \t\f\r\n]*+>)?+(?:[ \t\f\r\n]*+[.][ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]*+<[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+)(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+))*+[ \t\f\r\n]*+>)?+)*+(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]*+[.][ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)*+(?:[ \t\f\r\n]*+<[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+)(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+))*+[ \t\f\r\n]*+>)?+(?:[ \t\f\r\n]*+[.][ \t\f\r\n]*+\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+(?:[ \t\f\r\n]*+<[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+)(?:[ \t\f\r\n]*+,[ \t\f\r\n]*+(?:\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+|[?][ \t\f\r\n]*+(?:(?:extends|super)[ \t\f\r\n]++\p{javaJavaIdentifierStart}\p{javaJavaIdentifierPart}*+)?+))*+[ \t\f\r\n]*+>)?+)*+)*+)[ \t\f\r\n]*+)?+[{]

通过逐块构建图案来生成。我引用Java SE 7的Java语言规范来获得NormalClassDeclarationNormalInterfaceDeclaration的语法。

以下是生成上述怪物的代码,包括有关跳过哪种模式的注释:

// All rules never end with WhiteSpace. This allows us to check for error easier.

final static String Identifier = "\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*+";
// Heuristic to exclude any position where adding space would split an Identifier or a keyword apart
final static String TokenBoundary = "(?:(?<!\\p{javaJavaIdentifierPart})|(?!\\p{javaJavaIdentifierPart}))";

// Optional whitespace
final static String WhiteSpaceOpt = "[ \\t\\f\\r\\n]*+";
// Compulsory whitespace
final static String WhiteSpaceCom = "[ \\t\\f\\r\\n]++";

final static String MarkerAnnotation = "@" + WhiteSpaceOpt + Identifier;
// Skipped: SingleElementAnnotation, NormalAnnotation
// Can't be included due to middle recursion
final static String Annotation = MarkerAnnotation;

final static String ClassModifier = "(?:public|protected|private|abstract|static|final|strictfp|" + Annotation + ")";
// Since the declaration
//     public@Deprecated ...
// is allowed, the space between modifiers is optional.
// Since allowing the space to be optional recognizes this invalid declaration
//     publicstatic ...
// we need to assert boundary between 2 class modifiers
final static String ClassModifiers = ClassModifier + "(?:" + WhiteSpaceOpt + TokenBoundary + ClassModifier + ")*+";

final static String TypeVariable = Identifier;

// Skipped: ArrayType, ClassOrInterfaceType
// Can't be included, since the grammar is context-free, and this is where middle recursion occurs
final static String ReferenceType = TypeVariable;
final static String WildcardBounds = "(?:extends|super)" + WhiteSpaceCom + ReferenceType;
final static String Wildcard = "[?]" + WhiteSpaceOpt + "(?:" + WildcardBounds + ")?+";
final static String TypeArgument = "(?:" + ReferenceType + "|" + Wildcard + ")";
final static String TypeArgumentList = TypeArgument + "(?:" + WhiteSpaceOpt + "," + WhiteSpaceOpt + TypeArgument + ")*+";
final static String TypeArguments = "<" + WhiteSpaceOpt + TypeArgumentList + WhiteSpaceOpt + ">";

final static String TypeName = Identifier + "(?:" + WhiteSpaceOpt + "[.]" + WhiteSpaceOpt + Identifier + ")*+";
// Expanded definition of ClassOrInterfaceType = ClassType = InterfaceType
//     ClassOrInterfaceType -> TypeName  TypeArguments<opt>
//     ClassOrInterfaceType -> ClassOrInterfaceType . Identifier TypeArguments<opt>
final static String ClassType = TypeName + "(?:" + WhiteSpaceOpt + TypeArguments + ")?+" + 
    "(?:" + WhiteSpaceOpt + "[.]" + WhiteSpaceOpt + Identifier + "(?:" + WhiteSpaceOpt + TypeArguments + ")?+" + ")*+";
// Definition of ClassType and InterfaceType are identical
final static String InterfaceType = ClassType;
final static String ClassOrInterfaceType = ClassType;

final static String TypeBound = "extends" + WhiteSpaceCom + "(?:" + ClassOrInterfaceType + "|" + TypeVariable + ")";
final static String TypeParameter = TypeVariable + "(?:" + WhiteSpaceCom + TypeBound + ")?+";
final static String TypeParameterList = TypeParameter + "(?:" + WhiteSpaceOpt + "," + WhiteSpaceOpt + TypeParameter + ")*+";
final static String TypeParameters = "<" + WhiteSpaceOpt + TypeParameterList + WhiteSpaceOpt + ">";

final static String Super = "extends" + WhiteSpaceCom + ClassType;

final static String InterfaceTypeList = InterfaceType + "(?:" + WhiteSpaceOpt  + "," + WhiteSpaceOpt + InterfaceType + ")*+";
final static String Interfaces = "implements" + WhiteSpaceCom + InterfaceTypeList;

final static String NormalClassDeclaration =
    // Annotation in its fullest form can end in ), so WhiteSpaceOpt is used here for pedantic
    // It can be changed to WhiteSpaceCom to save the TokenBoundary check, since current definition always end with Identifier character
    "(?:" + "(" + ClassModifiers + ")" + WhiteSpaceOpt + ")?+" +
    TokenBoundary + "class" + WhiteSpaceCom +
    // WhiteSpaceOpt is used here, since TypeParameters starts with < and no whitespace is needed to delimit
    "(" + Identifier + ")" + WhiteSpaceOpt + 
    "(?:" + "(" + TypeParameters + ")" + WhiteSpaceOpt + ")?+" +
    // As the result, we need to check for boundary before "extends" and "implements"
    TokenBoundary + "(?:" + "(" + Super + ")"+ WhiteSpaceOpt + ")?+" +
    TokenBoundary + "(?:" + "(" + Interfaces + WhiteSpaceOpt + ")" + ")?+[{]";
    // ClassBody is skipped, and only opening bracket { is matched

final static String InterfaceModifier = "(?:public|protected|private|abstract|static|strictfp|" + Annotation + ")";
// Same as ClassModifiers, except that "final" is no longer a valid modifier
final static String InterfaceModifiers = InterfaceModifier + "(?:" + WhiteSpaceOpt + TokenBoundary + InterfaceModifier + ")*+";

final static String ExtendsInterfaces = "extends" + WhiteSpaceCom + InterfaceTypeList;

final static String NormalInterfaceDeclaration = 
    "(?:" + "(" + InterfaceModifiers + ")" + WhiteSpaceOpt + ")?+" +
    TokenBoundary + "interface" + WhiteSpaceCom +
    // WhiteSpaceOpt is used here, since TypeParameters starts with < and no whitespace is needed to delimit
    "(" + Identifier + ")" + WhiteSpaceOpt + 
    "(?:" + "(" + TypeParameters + ")" + WhiteSpaceOpt + ")?+" +
    // As the result, we need to check for boundary before "extends" here
    TokenBoundary + "(?:" + "(" + ExtendsInterfaces + ")" + WhiteSpaceOpt + ")?+[{]";

我很清楚CamelCase以及一些常数因复数而不同的事实是不合适的,但它提供了更好的映射到JLS中定义的语法。

以下是ideone上的演示。

答案 1 :(得分:1)

更改您的正则表达式,如下所示,以匹配两种类型的字符串格式。

line.matches("(?:public|protected|private|static)\\s+(?:class|interface)\\s+\\w+\\s*\\{");

示例:

String s1 = "public interface IGame {";
String s2 = "private class Game {";
System.out.println(s1.matches("(?:public|protected|private|static)\\s+(?:class|interface)\\s+\\w+\\s*\\{"));
System.out.println(s2.matches("(?:public|protected|private|static)\\s+(?:class|interface)\\s+\\w+\\s*\\{"));

输出:

true
true