Hong Kong HKID Number Regex for Java
/^([A-Z]{1,2})([0-9]{6})\(([0-9A])\)$/iWhat this pattern does
This page provides a well-structured, multi-part regular expression for matching hong kong hkid number, ported and verified for Java. Identity and credential patterns need both correctness and safety, since they're frequent targets for adversarial input. The snippet below is ready to drop into your Java project — whether you're validating in a Spring Boot controller, a Jakarta EE service, or a standalone utility class.
Java Implementation
// Hong Kong HKID Number
// ReDoS-safe | RegexVault — Identity & PII > National Identity Numbers
import java.util.regex.Pattern;
public class HongKongHkidNumberValidator {
private static final Pattern PATTERN =
Pattern.compile("^([A-Z]{1,2})([0-9]{6})\\(([0-9A])\\)$");
public static boolean validate(String input) {
return PATTERN.matcher(input).matches();
}
// Example
public static void main(String[] args) {
System.out.println(validate("A123456(7)")); // true
}
}Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
A123456(7) | A1234567 |
AB123456(3) | A12345(7) |
Z999999(A) | A1234567(7) |
A123456(0) | 123456(7) |
| — | A123456(B) |
When to use this pattern
This pattern is drawn from the Identity & PII > National Identity Numbers category and carries a ReDoS-safe certification. That matters for Java developers because critical in Java applications since the JVM regex engine uses backtracking and is susceptible to ReDoS without careful pattern design. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
Check character A represents 10 in the checksum. One-letter prefixes (A-Z) are issued chronologically; two-letter prefixes (AA-ZZ) started when the single-letter range was exhausted. Some older cards use lowercase or different formats.
Technical Notes
Structure: 1-2 uppercase letters + 6 digits + check character (0-9 or A) in parentheses. A single letter prefix is padded with a space for checksum calculation. The check character is computed via weighted sum mod 11.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern