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 Java
Phone number regex Java
IP address regex Java
Date regex Java
URL regex Java
Numbers only regex (digits only) Java
UUID regex Java
Regex match words Java
ZIP code regex Java
GUID regex Java
Password regex Java
HTML regex Java
SSN regex Java
XML regex Java
Mac address regex Java
Street address regex Java

Numbers only regex (digits only) Java

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

Basic numbers only regex

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

Pattern.compile("^\\d+$")
Test it!
/^\d+$/

True

False

Enter a text in the input above to see the result

Example code in Java:

import java.util.regex.Pattern;
import java.util.regex.MatchResult;
import java.util.Arrays;

public class Main {

    public static void main(String []args) {
        // Validate number
        boolean isMatch = Pattern.compile("^\\d+$")
               .matcher("42")
               .find(); 
        System.out.println(isMatch); // prints true
        
        // Extract number from a string
        String[] matches = Pattern.compile("\\d+")
                          .matcher("Your message was viewed 203 times.")
                          .results()
                          .map(MatchResult::group)
                          .toArray(String[]::new);
        System.out.println(Arrays.toString(matches)); // prints [203]
    }
}

Real number regex

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

Pattern.compile("^(?:-(?:[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 Java:

import java.util.regex.Pattern;
import java.util.regex.MatchResult;
import java.util.Arrays;

public class Main {

    public static void main(String []args) {
        // Validate real number
        boolean isMatch = Pattern.compile("^(?:-(?:[1-9](?:\\d{0,2}(?:,\\d{3})+|\\d*))|(?:0|(?:[1-9](?:\\d{0,2}(?:,\\d{3})+|\\d*))))(?:.\\d+|)$")
               .matcher("121220.22")
               .find(); 
        System.out.println(isMatch); // prints true
        
        // Extract real number from a string
        String[] matches = Pattern.compile("(?:-(?:[1-9](?:\\d{0,2}(?:,\\d{3})+|\\d*))|(?:0|(?:[1-9](?:\\d{0,2}(?:,\\d{3})+|\\d*))))(?:.\\d+|)")
                          .matcher("Pi equals to 3.14")
                          .results()
                          .map(MatchResult::group)
                          .toArray(String[]::new);
        System.out.println(Arrays.toString(matches)); // prints [3.14]
    }
}
Test it!

True

False

Enter a text in the input above to see the result

Notes on number only regex validation

In Java you can also validate number by trying to parse it:

try {
    double d = Double.parseDouble("1.12");
} catch (NumberFormatException nfe) {
    return false;
}

Create an internal tool with UI Bakery

Discover UI Bakery – an intuitive visual internal tools builder.

Try it now