Fundamentals

Number Regex: Integers, Signed Values, and Decimals

“A number” can mean digits only, a signed integer, a decimal, or a value with fixed precision. Define the accepted syntax before writing the pattern.

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

Digits only

Anchors plus + require the entire value to contain one or more digits.

Regex: ^\d+$

Optional sign

[+-]? allows one optional plus or minus sign before the digits.

Regex: ^[+-]?\d+$

Simple decimal

An optional non-capturing decimal group can handle basic dot-separated decimals.

Regex: ^[+-]?\d+(?:\.\d+)?$

Convert after validation

Regex checks syntax. Convert the value to a numeric type afterward to apply minimum, maximum, precision, and business rules.

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