正则表达式匹配完全限定类名 [英] Regular expression matching fully qualified class names
问题描述
在文本中匹配完全限定的Java类名称的最佳方法是什么?
示例: java.lang.Reflect
, java.util.ArrayList
, org.hibernate.Hibernate
。
Java完全限定类名(让我们说N)具有结构
NNNN
N部分必须是Java标识符。 Java标识符不能以数字开头,但在初始字符后,它们可以使用字母和数字,下划线或美元符号的任意组合:
------------------------ -----------------------
NN
它们也不能是保留字(如 import
, true
或 null
)。如果您只想检查合理性,以上就足够了。
Java标识符可能包含任何Unicode字符,而不是包含任何Unicode字符的仅拉丁。如果你也想检查一下,使用Unicode字符类:
([\p {Letter} _ $] [\p {Letter} \p {Number} _ $] * \。)* [\p {Letter} _ $] [\p {Letter} \p {Number} _ $] *
或简称
([\p {L} _ $] [\p {L} \p {N} _ $] * \。)* [\p {L} _ $] \p {L} \p {N} _ $] *
Java语言规范(第3.8节)具有有效标识符名称的所有详细信息。 p>
另请参阅此问题的答案: Java Unicode变量名
What is the best way to match fully qualified Java class name in a text?
Examples: java.lang.Reflect
, java.util.ArrayList
, org.hibernate.Hibernate
.
A Java fully qualified class name (lets say "N") has the structure
N.N.N.N
The "N" part must be a Java identifier. Java identifiers cannot start with a number, but after the initial character they may use any combination of letters and digits, underscores or dollar signs:
([a-zA-Z_$][a-zA-Z\d_$]*\.)*[a-zA-Z_$][a-zA-Z\d_$]*
------------------------ -----------------------
N N
They can also not be a reserved word (like import
, true
or null
). If you want to check plausibility only, the above is enough. If you also want to check validity, you must check against a list of reserved words as well.
Java identifiers may contain any Unicode letter instead of "latin only". If you want to check for this as well, use Unicode character classes:
([\p{Letter}_$][\p{Letter}\p{Number}_$]*\.)*[\p{Letter}_$][\p{Letter}\p{Number}_$]*
or, for short
([\p{L}_$][\p{L}\p{N}_$]*\.)*[\p{L}_$][\p{L}\p{N}_$]*
The Java Language Specification, (section 3.8) has all details about valid identifier names.
Also see the answer to this question: Java Unicode variable names
这篇关于正则表达式匹配完全限定类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!