UK Phone Number Regex for Java
/^(?:\+44|0)(?:\s|-)?(?:[1-9][0-9]{1,4})(?:\s|-)?(?:[0-9]{3,4})(?:\s|-)?(?:[0-9]{3,4})$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching uk phone number, ported and verified for Java. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. 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
// UK Phone Number
// ReDoS-safe | RegexVault — Localization > Phone Numbers
import java.util.regex.Pattern;
public class UkPhoneNumberValidator {
private static final Pattern PATTERN =
Pattern.compile("^(?:\\+44|0)(?:\\s|-)?(?:[1-9][0-9]{1,4})(?:\\s|-)?(?:[0-9]{3,4})(?:\\s|-)?(?:[0-9]{3,4})$");
public static boolean validate(String input) {
return PATTERN.matcher(input).matches();
}
// Example
public static void main(String[] args) {
System.out.println(validate("+44 20 7946 0958")); // true
}
}Test Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
+44 20 7946 0958 | +1 20 7946 0958 |
020 7946 0958 | 44 20 7946 0958 |
+447911123456 | +44 (0) 20 7946 |
07911 123456 | — |
0800 123 456 | — |
020-7946-095 | — |
When to use this pattern
This pattern is drawn from the Localization > Phone 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
UK numbers vary in length depending on area code — London has an 8-digit local number, while rural areas may have 7 or even 6. This variability makes regex validation approximate at best.
Technical Notes
UK uses a complex numbering plan. 020 = London, 0161 = Manchester, 07xxx = mobile, 0800 = freephone, 01xxx/02xxx = geographic. The (0) is sometimes shown in international format (+44 (0)20...) but not dialed.
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