Recipes

Phone Number Regex: Practical Patterns and Formatting Choices

Phone numbers are a good example of why requirements must come before Regex. Country, separators, optional spaces, and storage format all change the correct pattern.

Before you start: test every pattern with examples that should match and examples that should fail.

A strict formatted example

If your product requires a fixed mask, anchor the whole value.

Regex: ^\(\d{2}\) \d{5}-\d{4}$\nExample: (71) 99999-1234

Optional punctuation increases complexity

The more input formats you accept, the harder the pattern becomes to read. Many systems normalize non-digits first and validate the normalized value.

International phone numbers

A single universal Regex becomes fragile because numbering plans vary. E.164 storage and specialized phone libraries are usually safer for international applications.

Validation and user experience

Do not punish users while they are still typing. Show a clear example, consider input masking, and validate when the entry is complete.

Next step: test it for real

Use the Playground to experiment with the pattern, then practice in the game with feedback on false positives and false negatives.

Practice in the gameTest in Playground

Keep learning

← Back to learning hub