REGEXVAULTv2.0
Localization/Phone Numbers
Verified Safe

US/Canada Phone Number (NANP) Regex for Java

/^(?:\+1[\s.-]?)?(?:\(([2-9][0-9]{2})\)|([2-9][0-9]{2}))[\s.-]?([2-9][0-9]{2})[\s.-]?([0-9]{4})$/

What this pattern does

This page provides a comprehensive, battle-tested regular expression for matching us/canada phone number (nanp), 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

Java
// US/Canada Phone Number (NANP)
// ReDoS-safe | RegexVault — Localization > Phone Numbers

import java.util.regex.Pattern;

public class UscanadaPhoneNumberNanpValidator {
    private static final Pattern PATTERN =
        Pattern.compile("^(?:\\+1[\\s.-]?)?(?:\\(([2-9][0-9]{2})\\)|([2-9][0-9]{2}))[\\s.-]?([2-9][0-9]{2})[\\s.-]?([0-9]{4})$");

    public static boolean validate(String input) {
        return PATTERN.matcher(input).matches();
    }

    // Example
    public static void main(String[] args) {
        System.out.println(validate("+1 212-555-0100")); // true
    }
}

Test Cases

Matches (Valid)
Rejects (Invalid)
+1 212-555-0100112-555-0100
212-555-0100212-055-0100
(212) 555-0100212-555-010
212.555.0100+44 212-555-0100
2125550100800-1234567

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

Canada shares the +1 country code with the US. Area codes do not reliably indicate country — some Canadian area codes (e.g., 416=Toronto) look identical to US codes in format.

Technical Notes

NANP area codes and exchange codes cannot start with 0 or 1. Capture groups: 1/2=area code, 3=exchange, 4=subscriber. 555-0100 through 555-0199 are reserved for fiction. Toll-free: 800, 888, 877, 866, 855, 844, 833.

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