Chun ping Wang Chun ping Wang - 8 months ago 35
Java Question

Regular expression matching fully qualified class names

What is the best way to match fully qualified Java class name in a text?




A Java fully qualified class name (lets say "N") has the structure


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:

------------------------    -----------------------
          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:


or, for short


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