LogoReturn to Home RegEx
Integrations
All integrations
AWS API
AWS Lambda
DynamoDB
Oracle
Redshift
Snowflake
GraphQL
Supabase
Twilio
Azure Blob Storage
Slack
SendGrid
Generic HTTP API
AWS S3
Stripe
Microsoft SQL
Salesforce
PostgreSQL
MySQL
MongoDB
HubSpot
Google Sheets
Google BigQuery
Firebase
Airtable
Integrations
About UI Bakery
Log in
Request UI Bakery demo
RegEx library
Email regex PHP
Phone number regex PHP
IP address regex PHP
Date regex PHP
URL regex PHP
Numbers only regex (digits only) PHP
UUID regex PHP
Regex match words PHP
ZIP code regex PHP
GUID regex PHP
Password regex PHP
HTML regex PHP
SSN regex PHP
XML regex PHP
Mac address regex PHP
Street address regex PHP

Numbers only regex (digits only) PHP

Numbers only (or digits only) regular expressions can be used to validate if a string contains only numbers.

Discover UI Bakery – an intuitive visual internal tools builder. Try it now!
JavaScript
Python
Java
C#
PHP
No items found.

Basic numbers only regex

Below is a simple regular expression that allows validating if a given string contains only numbers:

"/^\\d+$/"
Test it!
/^\d+$/

True

False

Enter a text in the input above to see the result

Example code in PHP:

// Validate if a string is a valid number
$number_validation_regex = "/^\\d+$/"; 
echo preg_match($number_validation_regex, '42'); // returns 1

// Extract number from a string
$extract_number_pattern = "/\\d+/";
$string_to_match = 'Your message was viewed 203 times.';
preg_match_all($extract_number_pattern, $string_to_match, $matches);
print_r($matches[0])// matches[0] is ['203']

Real number regex

Real number regex can be used to validate or exact real numbers from a string.

"/^(?:-(?:[1-9](?:\\d{0,2}(?:,\\d{3})+|\\d*))|(?:0|(?:[1-9](?:\\d{0,2}(?:,\\d{3})+|\\d*))))(?:.\\d+|)$/"
Test it!
/^(?:-(?:[1-9](?:\d{0,2}(?:,\d{3})+|\d*))|(?:0|(?:[1-9](?:\d{0,2}(?:,\d{3})+|\d*))))(?:.\d+|)$/

True

False

Enter a text in the input above to see the result

Example code in PHP:

// Validate if a string is a valid real number
$number_validation_regex = "/^(?:-(?:[1-9](?:\\d{0,2}(?:,\\d{3})+|\\d*))|(?:0|(?:[1-9](?:\\d{0,2}(?:,\\d{3})+|\\d*))))(?:.\\d+|)$/"; 
echo preg_match($number_validation_regex, '121220.22'); // returns 1

// Extract real number from a string
$extract_number_pattern = "/(?:-(?:[1-9](?:\\d{0,2}(?:,\\d{3})+|\\d*))|(?:0|(?:[1-9](?:\\d{0,2}(?:,\\d{3})+|\\d*))))(?:.\\d+|)/";
$string_to_match = 'Pi equals to 3.14';
preg_match_all($extract_number_pattern, $string_to_match, $matches);
print_r($matches[0])// matches[0] is ['3.14']
Test it!

True

False

Enter a text in the input above to see the result

Notes on number only regex validation

In PHP you can also validate number by using is_numeric function:

is_numeric("123") // returns TRUE

Create an internal tool with UI Bakery

Discover UI Bakery – an intuitive visual internal tools builder.

Try it now