簡體   English   中英

Java正則表達式,用於檢測類/接口/等聲明

[英]Java Regular Expression for detecting class/interface/etc declaration

我正在嘗試創建一個檢測新類的正則表達式,例如:

public interface IGame {

要么

private class Game {

這是我到目前為止所沒有的,但是沒有檢測到:

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

任何人都可以給我一些指示嗎?

此正則表達式是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的演示。

如下更改您的正則表達式以匹配兩種類型的字符串格式。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM