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

Numbers only regex (digits only) C#

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:

new Regex("^\\d+$")
Test it!
/^\d+$/

True

False

Enter a text in the input above to see the result

Example code in C#:

using System.Text.RegularExpressions;
using System.Linq;
using System;
                    
public class Program
{
    public static void Main()
    {
        // Validate number
        Regex validateNumberRegex = new Regex("^\\d+$");
        Console.WriteLine(validateNumberRegex.IsMatch("42"));  // prints True
        
        // Extract number from a string
        Regex extractNumberrRegex = new Regex("\\d+");
        string [] extracted = extractNumberrRegex.Matches("Your message was viewed 203 times.")
            .Cast<Match>()
            .Select(m => m.Value) 
            .ToArray(); 
        Console.WriteLine(String.Join(",", extracted)); // 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 C#:

using System.Text.RegularExpressions;
using System.Linq;
using System;
                    
public class Program
{
    public static void Main()
    {
        // Validate real number
        Regex validateNumberRegex = new Regex("^(?:-(?:[1-9](?:\\d{0,2}(?:,\\d{3})+|\\d*))|(?:0|(?:[1-9](?:\\d{0,2}(?:,\\d{3})+|\\d*))))(?:.\\d+|)$");
        Console.WriteLine(validateNumberRegex.IsMatch("121220.22"));  // prints True
        
        // Extract real number from a string
        Regex extractNumberRegex = new Regex("(?:-(?:[1-9](?:\\d{0,2}(?:,\\d{3})+|\\d*))|(?:0|(?:[1-9](?:\\d{0,2}(?:,\\d{3})+|\\d*))))(?:.\\d+|)");
        string [] extracted = extractNumberRegex.Matches("Pi equals to 3.14")
            .Cast<Match>()
            .Select(m => m.Value) 
            .ToArray(); 
        Console.WriteLine(String.Join(",", extracted)); // 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 C# you can also validate number by trying to parse it:

int.TryParse("123");

Create an internal tool with UI Bakery

Discover UI Bakery – an intuitive visual internal tools builder.

Try it now